批量插入数据

1、SqlBulkCopy插入数据

 1 /// <summary>
 2         /// 使用SqlBulkCopy方式插入数据
 3         /// </summary>
 4         /// <param name="dataTable"></param>
 5         /// <param name="tableName">目标表名称</param>
 6         /// <param name="connectionString">数据库连接字符串</param>
 7         /// <returns></returns>
 8         private static long SqlBulkCopyInsert(DataTable dataTable, string tableName, string connectionString)
 9         {
10             Stopwatch stopwatch = new Stopwatch();//用于当前实例完成时的所用时间
11             stopwatch.Start();
12 
13             SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(connectionString);
14             sqlBulkCopy.DestinationTableName = tableName;
15             sqlBulkCopy.BatchSize = dataTable.Rows.Count;
16             SqlConnection sqlConnection = new SqlConnection(connectionString);
17             sqlConnection.Open();
18             if (dataTable.Rows.Count > 0)
19             {
20                 sqlBulkCopy.WriteToServer(dataTable);
21             }
22             sqlBulkCopy.Close();
23             sqlConnection.Close();
24 
25             stopwatch.Stop();
26             return stopwatch.ElapsedMilliseconds;
27         }

 

posted @ 2015-09-12 17:37  细品人生  阅读(233)  评论(0编辑  收藏  举报