码迷,mamicode.com
首页 > 其他好文 > 详细

批量插入数据

时间:2015-09-12 18:56:06      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

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         }

 

批量插入数据

标签:

原文地址:http://www.cnblogs.com/ganqiyin/p/4803300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!