码迷,mamicode.com
首页 > 数据库 > 详细

把dataTable表批量的写入数据库

时间:2016-08-30 17:25:07      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

 

connectionStr:链接字符串;dataTableName数据库中的表;sourceDataTable dataTable的名称

public static void SqlBulkCopyByDataTable(string connectionStr, string dataTableName, DataTable sourceDataTable, int batchSize = 100000)
{
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionStr))
{
using (System.Data.SqlClient.SqlBulkCopy sqlBulkCopy = new System.Data.SqlClient.SqlBulkCopy(connectionStr, System.Data.SqlClient.SqlBulkCopyOptions.UseInternalTransaction))
{
try
{
sqlBulkCopy.DestinationTableName = dataTableName;
sqlBulkCopy.BatchSize = batchSize;
for (int i = 0; i < sourceDataTable.Columns.Count; i++)
{
sqlBulkCopy.ColumnMappings.Add(sourceDataTable.Columns[i].ColumnName, sourceDataTable.Columns[i].ColumnName);
}
sqlBulkCopy.WriteToServer(sourceDataTable);
}
catch (Exception ex)
{

throw ex;
}
}
}
}

把dataTable表批量的写入数据库

标签:

原文地址:http://www.cnblogs.com/it-xcn/p/5822378.html

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