sql server批量写入数据

sql server批量写入数据
 1 public bool SaveCompareResult(List<CompareResult> c)
 2         {
 3             bool flag = false;
 4             using (var db = new AOPContext())
 5             {
 6                 SqlConnection sqlConn = new SqlConnection(db.Database.Connection.ConnectionString); ;
 7                 SqlBulkCopy bulkCopy = new SqlBulkCopy(db.Database.Connection.ConnectionString);;
 8                 try
 9                 {
10                     bulkCopy.DestinationTableName = "CompareResult"; //要写入的表的名字
11                     bulkCopy.BatchSize = c.Count;  
12                     DataTable dt = new DataTable();
13                     dt.Columns.AddRange(new DataColumn[]{  
14                                         new DataColumn("CID",typeof(Guid)), 
15                                         new DataColumn("RID",typeof(Guid)),
16                                         new DataColumn("CType",typeof(int)), 
17                                         new DataColumn("CChildType",typeof(int)),
18                                         new DataColumn("Value1",typeof(string)), 
19                                         new DataColumn("Value2",typeof(string)), 
20                                         new DataColumn("Value3",typeof(string)), 
21                                         new DataColumn("Value4",typeof(string)), 
22                                         new DataColumn("Value5",typeof(int)), 
23                                         new DataColumn("FileType",typeof(string)), 
24                                         new DataColumn("Operator",typeof(string)), 
25                                         new DataColumn("OperateDate",typeof(DateTime))});
26 
27                     foreach (CompareResult cr in c)
28                     {
29                         DataRow r = dt.NewRow();
30                         r[0] = cr.CID;
31                         r[1] = cr.RID;
32                         r[2] = cr.CType;
33                         r[3] = cr.CChildType;
34                         r[4] = cr.Value1;
35                         r[5] = cr.Value2;
36                         r[6] = cr.Value3;
37                         r[7] = cr.Value4;
38                         r[8] = cr.Value5;
39                         r[9] = cr.FileType;
40                         r[10] = cr.Operator;
41                         r[11] = cr.OperateDate;
42                         dt.Rows.Add(r);  
43                     }
44                     sqlConn.Open();
45                     bulkCopy.WriteToServer(dt);
46                     flag = true;
47                 }
48                 catch (Exception ex)
49                 {
50                     LoggerHelper.Error("SaveCompareResult执行出错: ", ex);
51                 }
52                 finally
53                 {
54                     sqlConn.Close();
55                     if (bulkCopy != null)
56                         bulkCopy.Close();
57                 }
58             }
59             return flag;
60         }
sql server批量写入数据

 

 

 

参考 http://blog.csdn.net/tjvictor/article/details/4360030

sql server批量写入数据,布布扣,bubuko.com

sql server批量写入数据

上一篇:LINUX核心命令实战总结十——进程管理命令


下一篇:linux操作指南-03