http://clingingboy.cnblogs.com/archive/2006/04/29/389039.html
下面代码一次是5000个,具体在你那数值是多少可以测试一下
/// <summary>
/// 批量更新数据(每批次5000)
/// </summary>
/// <param name="connString">数据库链接字符串</param>
/// <param name="table"></param>
public static void Update(string connString, DataTable table)
{
SqlConnection conn = new SqlConnection(connString)
SqlCommand comm = conn.CreateCommand()
comm.CommandTimeout = _CommandTimeOut
comm.CommandType = CommandType.Text
SqlDataAdapter adapter = new SqlDataAdapter(comm)
SqlCommandBuilder commandBulider = new SqlCommandBuilder(adapter)
commandBulider.ConflictOption = ConflictOption.OverwriteChanges
try
{
conn.Open()
//设置批量更新的每次处理条数
adapter.UpdateBatchSize = 5000
adapter.SelectCommand.Transaction = conn.BeginTransaction()/////////////////开始事务
if (table.ExtendedProperties["SQL"] != null)
{
adapter.SelectCommand.CommandText = table.ExtendedProperties["SQL"].ToString()
}
adapter.Update(table)
adapter.SelectCommand.Transaction.Commit()/////提交事务
}
catch (Exception ex)
{
if (adapter.SelectCommand != null &&adapter.SelectCommand.Transaction != null)
{
adapter.SelectCommand.Transaction.Rollback()
}
throw ex
}
finally
{
conn.Close()
conn.Dispose()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)