ASP.NET(C#) 使用 SqlBulkCopy 实现批量插入SQL(快捷简单)

业务需要,系统在处理数据时,每暂存一列数据将他插入到右侧的表格中,再执行批量保存,如图所示:

ASP.NET(C#) 使用 SqlBulkCopy 实现批量插入SQL(快捷简单)

//以前的做法可能是生成一堆 insert into xx values xxx 的sql语句,在程序中去一次执行来实现。这种做法在数据量大的情况下,简直不要太慢!不建议使用

//在.NET里可以使用SqlBulkCopy来一次性插入多条数据,我们只需要使用WriteToServer给它传递一个DataTable类型,他就会自动实现批量插入

代码如下,我的代码需要处理一些数据,如果看起来麻烦请看代码下方的Demo:

 1         protected void Button5_Click1(object sender, EventArgs e)
 2         {
 3             DataTable dt = new DataTable();
 4             CJJLModel model = new CJJLModel();
 5             //前台获取到的table所有数据转为List
 6             model.CjjlList = Newtonsoft.Json.JsonConvert.DeserializeObject<cjjLists>(this.HidValue.Value);
 7             Tool.SystemPage UserDal = new Tool.SystemPage();
 8             string UserData = UserDal.GetPageUserData();
 9             string[] ArrUser = UserData.Split(',');
10             for (int i = 0; i < model.CjjlList.Count; i++)
11             {
12                 model.CjjlList[i].Creater = ArrUser[0];
13                 model.CjjlList[i].CreateData = DateTime.Now;
14                 model.CjjlList[i].CreateTime = DateTime.Now;
15             }
16             //list转DataTable
17             dt =  ToDataTable(model.CjjlList);
18             using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SqlHelper.SCConnString))
19             {
20                 bulkCopy.DestinationTableName = "SC_CJ_CJJL";   //指定要插入的目标表
21 
22                 //DataTable列名与数据库列名的映射
23                 bulkCopy.ColumnMappings.Add("ID", "ID");
24                 bulkCopy.ColumnMappings.Add("Location", "Location");
25                 bulkCopy.ColumnMappings.Add("MLDM", "MLDM");
26                 bulkCopy.ColumnMappings.Add("MLMC", "MLMC");
27                 bulkCopy.ColumnMappings.Add("GGMC", "GGMC");
28                 bulkCopy.ColumnMappings.Add("XDL", "XDL");
29                 bulkCopy.ColumnMappings.Add("SJHL", "SJHL");
30                 bulkCopy.ColumnMappings.Add("ZPPS", "ZPPS");
31                 bulkCopy.ColumnMappings.Add("TMLKS", "TMLKS");
32                 bulkCopy.ColumnMappings.Add("SCDH", "SCDH");
33                 bulkCopy.ColumnMappings.Add("CHS", "CHS");
34                 bulkCopy.ColumnMappings.Add("BZ", "BZ");
35                 bulkCopy.ColumnMappings.Add("QWID", "QWID");
36                 bulkCopy.ColumnMappings.Add("ZCID", "ZCID");
37                 bulkCopy.ColumnMappings.Add("MRBBID", "MRBBID");
38                 bulkCopy.ColumnMappings.Add("KH", "KH");
39                 bulkCopy.ColumnMappings.Add("FDCS", "FDCS");
40                 bulkCopy.ColumnMappings.Add("YSDM", "YSDM");
41                 bulkCopy.ColumnMappings.Add("SYBW", "SYBW");
42                 bulkCopy.ColumnMappings.Add("CreateData", "CreateData");
43                 bulkCopy.ColumnMappings.Add("Creater", "Creater");
44                 bulkCopy.ColumnMappings.Add("CreateTime", "CreateTime");
45 
46                 bulkCopy.WriteToServer(dt);//写入到数据库中
47             }
48             Response.Write("<script>alert('保存成功');location.href='CJJLList.aspx';</script>");
49         }

Demo示例代码:

 1 protected void Button5_Click(object sender, EventArgs e)
 2 {
 3     //准备要批量插入的数据
 4     DataTable table = new DataTable();
 5     table.Columns.Add("Age");
 6     table.Columns.Add("Name");
 7 
 8     //添加测试数据
 9     DataRow row = table.NewRow();
10     row["Age"] = 28;
11     row["Name"] = "张三";
12     table.Rows.Add(row);
13 
14     DataRow row1 = table.NewRow();
15     row1["Age"] = 29;
16     row1["Name"] = "李四";
17     table.Rows.Add(row1);
18 
19 
20     string connectionStr = "数据连接字符串";
21     using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionStr))
22     {
23         bulkCopy.DestinationTableName = "Person";//在插入的目标表
24                                                  //DataTable列名与数据库列名的映射
25         bulkCopy.ColumnMappings.Add("Age", "Age");
26         bulkCopy.ColumnMappings.Add("Name", "Name");
27         bulkCopy.WriteToServer(table);//写入到数据库中
28     }
29     Response.Write("YES");
30 }

到此结束,谢谢各位~

 

 

上一篇:shape的简单用法


下一篇:echarts学习总结(二):一个页面存在多个echarts图形,图形自适应窗口大小