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

大数据批量插入数据库

时间:2015-11-27 14:56:41      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:

public void ExecuteBuklCopy(DataTable dt)
{
SqlConnection conn = new SqlConnection(connectionString );
if (dt == null || dt.Rows.Count <= 0 || String.IsNullOrEmpty ( dt.TableName ) )
{ return; }
if (conn.State != ConnectionState.Open)
{
conn.Open();
}

SqlTransaction sqlbulkTransaction = conn.BeginTransaction();
//请在插入数据的同时检查约束,如果发生错误调用sqlbulkTransaction事务
SqlBulkCopy copy = new SqlBulkCopy(conn, SqlBulkCopyOptions.CheckConstraints, sqlbulkTransaction);


copy.DestinationTableName = dt.TableName;
foreach (DataColumn dc in dt.Columns)
{
copy.ColumnMappings.Add(dc.ColumnName, dc.ColumnName);

}
try
{
//每次10000条
if (dt.Rows.Count > 10000)
{

System.Collections.Generic.List<DataRow> _list = new System.Collections.Generic.List<DataRow>();
int i = 0;
for (i = 0; i < dt.Rows.Count; i++)
{
if (i > 0 && i % 10000 == 0)
{
copy.WriteToServer(_list.ToArray());
sqlbulkTransaction.Commit();
_list.Clear();
}
//else
{
_list.Add(dt.Rows[i]);
}
}
if (_list.Count > 0)
{
copy.WriteToServer(_list.ToArray());
sqlbulkTransaction.Commit();
}
}
else
{
copy.WriteToServer(dt);
sqlbulkTransaction.Commit();
}
}
catch (Exception ex)
{
sqlbulkTransaction.Rollback();
Console.WriteLine(ex.ToString());
}
finally
{
copy.Close();
conn.Close();
}

}

大数据批量插入数据库

标签:

原文地址:http://www.cnblogs.com/zhshlimi/p/5000541.html

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