在ASP.NET 中如何做批量录入

解决方案 »

  1.   

    insert into tablename(字段)
    select 'aa' union all
    select 'bb' union all
    select 'cc' 是这样不?
    不太明白楼主要什么样的批量录入
      

  2.   

    .cs页面
    /// <summary>
        /// 删除
        /// </summary>
        public void DataGrid_Delete(object sender, DataGridCommandEventArgs e)
        {
            string id = DataGrid.DataKeys[(int)e.Item.ItemIndex].ToString();
            DataSet ds = ((DataSet)ViewState["ds"]);
            ds.Tables[0].Rows[(int)e.Item.ItemIndex].Delete() ;
            ds.Tables[0].AcceptChanges();
            ViewState["ds"] = ds;
            BindGrid();
        }    /// <summary>
        /// 添加
        /// </summary>
        protected void add_Click(object sender, System.EventArgs e)
        {
            addRow();
        }    //增加一个空行
        public void addRow()
        {
            DataRow newRow;
            int count = 0;
            DataSet ds = ((DataSet)ViewState["ds"]);
            if (ds != null)
            {
                DataView dv = ds.Tables[0].DefaultView;
                count = ds.Tables[0].Rows.Count;
                if (count > 0)
                {
                    if (ds.Tables[0].Rows[count-1]["NoticeMan"].ToString().Equals(""))
                    {                    DataGrid.DataSource = dv;
                        DataGrid.EditItemIndex = count - 1;
                        DataGrid.DataBind();
                        ViewState["ds"] = ds;
                        return;
                    }
                }
                count = ds.Tables[0].Rows.Count + 1;
           
                newRow = ds.Tables[0].NewRow();
                newRow["SetDetailID"] = count;
                newRow["NoticeMan"] = "";
                newRow["UnitName"] = "";
                newRow["PhoneNum"] = "";
                newRow["NoticeType"] = 0;
                ds.Tables[0].Rows.Add(newRow);
                dv = ds.Tables[0].DefaultView;
                dv.Sort = "SetDetailID";
                DataGrid.DataSource = dv;
                DataGrid.EditItemIndex = count - 1;
                DataGrid.DataBind();
                ViewState["ds"] = ds;
            }
        }    /// <summary>
        /// 修改
        /// </summary>
        public void DataGrid_Edit(object sender, DataGridCommandEventArgs e)
        {
            DataGrid.EditItemIndex = e.Item.ItemIndex;
     //       gEditIndex = e.Item.ItemIndex.ToString();
            BindGrid();
        }    /// <summary>
        /// 取消
        /// </summary>
        public void DataGrid_Cancel(object sender, DataGridCommandEventArgs e)
        {
            DataGrid.EditItemIndex = -1;
            BindGrid();
        }
        
        //增加序号,转换
        protected void DataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            DataGridItem item = e.Item;        ListItemType itemtype = e.Item.ItemType;        if (itemtype != ListItemType.Header && itemtype != ListItemType.Footer)
            {
                item.Cells[0].Text = (item.ItemIndex + 1).ToString();
                if (item.ItemType == ListItemType.EditItem)
                {
                   
                    DataRowView drv = (DataRowView)e.Item.DataItem;
                    ExtendWebControls.DropDownListExtend ctr = (ExtendWebControls.DropDownListExtend)e.Item.FindControl("DropDownListExtend1");
                    ctr.Attributes.Add("onkeyup", "mykeyup(this);");
                    ctr.Values = GetUserToHashTable(Session["UnitID"].ToString());
                    ctr.Text = drv["NoticeMan"].ToString();
                }           
                ImageButton  imgbutton = (ImageButton)e.Item.FindControl("Imagebutton3");
                if (imgbutton != null)
                {
                    imgbutton.Attributes.Add("onclick", "return checkgrid(this);");
                }            //更新checkboxList
                HtmlInputHidden hid = (HtmlInputHidden)e.Item.FindControl("hidNoticeType");
                CheckBoxList list = (CheckBoxList)e.Item.FindControl("cblNoticeType");
                if (hid.Value.Equals("1"))
                {
                    list.Items[0].Selected = true;
                }
                else if (hid.Value.Equals("2"))
                {
                    list.Items[1].Selected = true;
                }
                else if (hid.Value.Equals("3"))
                {
                    list.Items[0].Selected = true;
                    list.Items[1].Selected = true;
                }
                //选择框为不可选
                if (DataGrid.EditItemIndex != item.ItemIndex)
                {
                    list.Enabled = false;
                }
            }
        }    /// <summary>
        /// 更新
        /// </summary>
        public void DataGrid_Update(object sender, DataGridCommandEventArgs e)
        {
            string strErr;
            TextBox EditText = null;
            //取得当前行
            int row = Convert.ToInt32(e.Item.ItemIndex);
            //获取当前的主键
            int SetDetailID = Convert.ToInt32(DataGrid.DataKeys[row]);        ExtendWebControls.DropDownListExtend ctr = (ExtendWebControls.DropDownListExtend)e.Item.FindControl("DropDownListExtend1");
            string NoticeMan = ctr.Text;
            ////验证通知人
            //if (NoticeMan.Equals(""))
            //{
            //    strErr = "通知人不能为空";
            //    MagicAjax.AjaxCallHelper.Write("alert('" + strErr + "');");
            //    //需要重新绑定
            //    BindGrid();
            //    return;
            //}
            EditText = (TextBox)e.Item.FindControl("txtUnitName");
            string UnitName = EditText.Text;
            ////验证所属单位
            //if (UnitName.Equals(""))
            //{
            //    strErr = "所属单位不能为空";
            //    MagicAjax.AjaxCallHelper.Write("alert('" + strErr + "');");
            //    //需要重新绑定
            //    BindGrid();
            //    return;
            //}
            EditText = (TextBox)e.Item.FindControl("txtPhoneNum");
            string PhoneNum = EditText.Text;
            CheckBoxList list = (CheckBoxList)e.Item.FindControl("cblNoticeType");
            int NoticeType = 0;
            if (list.Items[0].Selected == true && list.Items[1].Selected == true)
            {
                NoticeType = 3;
            }
                
            else if (list.Items[0].Selected == true)
            {
                NoticeType = 1;
            }
            else if (list.Items[1].Selected == true)
            {
                NoticeType = 2;
            }        ////验证通知方式
            //if (NoticeType==0)
            //{
            //    strErr = "通知方式不能为空";
            //    MagicAjax.AjaxCallHelper.Write("alert('" + strErr + "');");
            //    //需要重新绑定
            //    BindGrid();
            //    return;
            //}        //用户帐号,在HASHTABLE的值里面去找
            ctr.Values = GetUserToHashTable(Session["UnitID"].ToString());        string NoticeAccount = "";        Hashtable hashtable = ctr.Values;
            foreach (DictionaryEntry de in hashtable)
            {
                string temp = de.Key.ToString();
                string[] tempary = temp.Split(',');
                if (tempary[1].Equals(NoticeMan))
                {
                    NoticeAccount = tempary[0];
                    break;
                }
            }        DataSet ds = (DataSet)ViewState["ds"];        //这边判断,用户姓名不能重复
            DataTable dt = ds.Tables[0];
            bool flag = true;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["NoticeMan"].ToString().Equals(NoticeMan) && row!=i)
                {
                    flag = false;
                    break;
                }
            }        if (flag)
            {
                DataRow updateRow = ds.Tables[0].Rows[row];
                updateRow["NoticeAccount"] = NoticeAccount;
                updateRow["NoticeMan"] = NoticeMan;
                updateRow["UnitName"] = UnitName;
                updateRow["PhoneNum"] = PhoneNum;
                updateRow["NoticeType"] = NoticeType;
                ds.Tables[0].Rows[row].AcceptChanges();
                ViewState["ds"] = ds;
                addRow();
            }
            else
            {
                strErr = "更新失败,用户名不能重复";
                MagicAjax.AjaxCallHelper.Write("alert('" + strErr + "');");
            }
        }
      

  3.   

    http://download.csdn.net/source/906316
      

  4.   

    拼装成多条insert形成一条strsql 没两条insert中间用分号做分隔符  更新数据库的时候只用这一条strsql  这样就不用写事务了  要么全部insert  要么一条都插入不了
    这样组合语句 
    strSQL = "";
    for (int j = 0; j < dtBocDetail.Rows.Count ; j++)
                            {
                                SystemBE.SDSBOCDetail detail = new SDSBOCDetail();
                                detail.B_BOCDictate = d.B_ID;
                                detail.B_Order = 0;
                                    strSQL += "Insert into SDS_BOCDetail values('" + detail.B_ID + "'," + detail.B_Order );";
                            }
                      return strSQL ;
      

  5.   

    把数据拼成XML,然后到存储过程分析这个XML,拆解成一条条的记录?
      

  6.   


    想不刷新就用js生成,保存时ajax回调给服务器端处理.
      

  7.   

    昏 把你想添加的sql语句 循环撒
      

  8.   


    var table = document.getElementById("tableid"); 
    var tr = table.insertRow();//添加tr 
    tr.className = "xxx"; 
    var td = tr.insertCell();//添加td//然后你再往创建出的td中添加input 
    var input = document.createElement("input"); 
    input.type = "text"; 
    input.id = "xxxx"; 
    input.name = "xxxx"; 
    input.style.width="95%"; 
    td.appendChild(input);
    ...
    ..
    以上就是创建的工作.
      

  9.   

    写完了,很匆忙,凑合看吧.算是提供个思路(实现刚才你在即时聊天中说的那功能)<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="test1.WebForm2" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <script src="Ajax.js" type="text/javascript"></script>
        <script type="text/javascript">
        function func()
        {
            var table = document.getElementById("test"); 
            var tr = table.insertRow();//添加tr 
            //tr.className = "xxx"; 
            var td1 = tr.insertCell();//添加td
            BuildText(td1,"newtext1");
            var td2 = tr.insertCell();//添加td
            BuildText(td2,"newtext2");                var url="WebForm2.aspx?type=getdata";     
            var ajax = new AjaxCore.Ajax("POST",url,receiveInfo,null);
            ajax.SendRequest();    }
        
        function receiveInfo(data)
        {
            if(data.responseText == "false")
            {}
            else if(data.responseText=="load")
            {}
            else
            {
                var str = data.responseText;
                var json = eval('('+str+')');
                
                document.getElementById('newtext1').value = json.td1;
                document.getElementById('newtext2').value = json.td2;
            }
        }    
        function BuildText(panel,id)
        {
            var input = document.createElement("input"); 
            input.type = "text"; 
            input.id = id;
            input.name = id;
            input.style.width="95%"; 
            panel.appendChild(input);
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <input type="button" onclick="func()" value="Add" />
        <table id="test" width="50%" border="1">
            <tr>
                <td>ttttA</td>
                <td>ttttB</td>
            </tr>
        </table>
        
       </div>
        </form>
    </body>
    </html>using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;namespace test1
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Request["type"] != null)
                {
                    if (Request["type"].ToString() == "getdata")
                    {
                        string json = "{{\"td1\":\"{0}\",\"td2\":\"{1}\"}}";
                        Response.Write(string.Format(json,"aaaaa","bbbbb"));
                        Response.End();
                    }
                   
                }
            }
        }
    }
    ajax.js文件
    var AjaxCore=new Object();
    AjaxCore.Ajax=function(type,url,onload,params,contentType){
        this.req=null;
        this.url=url;
        this.params=params;
        if(type){
            this.type=type;
        }else{
            this.type="GET";
        }
        if(!contentType && type=="POST"){
            this.contentType="application/x-www-form-urlencoded; charset=UTF-8";
        }else{
            this.contentType=contentType;
        }
        this.onload=onload;
        this.responseText=null;
        this.GetHttpRequest();
    }AjaxCore.Ajax.prototype.GetHttpRequest=function(){
        if(window.ActiveXObject){
            this.req=new ActiveXObject("Microsoft.XMLHTTP");
        }else if(window.XMLHttpRequest){
            this.req=new XMLHttpRequest();
        }
    }AjaxCore.Ajax.prototype.SendRequest=function(){
        var loader=this;
        this.req.onreadystatechange=function(){
            loader.OnReadyState.call(loader);
        }
        this.req.open(this.type,this.url,true);
        if(this.contentType){
            this.req.setRequestHeader("Content-Type",this.contentType);
        }
        this.req.send(this.params);
    }AjaxCore.Ajax.prototype.OnReadyState=function(){
        var ready=this.req.readyState;
        var data;
        if(ready==4){
            if(this.req.status==200 || this.req.status==0){
                this.responseText=this.req.responseText;
            }else{
                this.responseText="false";
            }
        }else{
            this.responseText="load";
        }
        this.onload.call(this,this);
    }
      

  10.   

    参考:
    http://blog.csdn.net/linaren/archive/2009/03/04/3957463.aspx
      

  11.   

    把数据写成txt,数据之间用特定的符号分割,
    上传后读取文件,分割成数据数组,然后循环插入数据库;