sql server中批量插入与更新两种解决方案分享(as
网络编程 2021-07-04 22:40www.168986.cn编程入门
xml和表值函数的相对复杂些这里简单贴一下bcp和SqlDataAdapter进行批量跟新插入方法,未经整理还望见谅
若只是需要大批量插入数据使用bcp是最好的,若需要插入、删除、更新建议使用SqlDataAdapter我测试过有很高的效率,一般情况下这两种就满足需求了
bcp方式
/// <summary>
/// 大批量插入数据(2000每批次)
/// 已采用整体事物控制
/// </summary>
/// <param name="connString">数据库链接字符串</param>
/// <param name="tableName">数据库服务器上目标表名</param>
/// <param name="dt">含有和目标数据库表结构完全一致(所包含的字段名完全一致即可)的DataTable</param>
public static void BulkCopy(string connString, string tableName, DataTable dt)
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlTransaction transaction = conn.BeginTransaction())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, transaction))
{
bulkCopy.BatchSize = 2000;
bulkCopy.BulkCopyTimeout = _CommandTimeOut;
bulkCopy.DestinationTableName = tableName;
try
{
foreach (DataColumn col in dt.Columns)
{
bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
}
bulkCopy.WriteToServer(dt);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
finally
{
conn.Close();
}
}
}
}
}
SqlDataAdapter
/// <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 m = conn.CreateCommand();
m.CommandTimeout = _CommandTimeOut;
m.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter(m);
SqlCommandBuilder mandBulider = new SqlCommandBuilder(adapter);
mandBulider.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();
}
}
bcp方式
代码如下:
/// <summary>
/// 大批量插入数据(2000每批次)
/// 已采用整体事物控制
/// </summary>
/// <param name="connString">数据库链接字符串</param>
/// <param name="tableName">数据库服务器上目标表名</param>
/// <param name="dt">含有和目标数据库表结构完全一致(所包含的字段名完全一致即可)的DataTable</param>
public static void BulkCopy(string connString, string tableName, DataTable dt)
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlTransaction transaction = conn.BeginTransaction())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, transaction))
{
bulkCopy.BatchSize = 2000;
bulkCopy.BulkCopyTimeout = _CommandTimeOut;
bulkCopy.DestinationTableName = tableName;
try
{
foreach (DataColumn col in dt.Columns)
{
bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
}
bulkCopy.WriteToServer(dt);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
finally
{
conn.Close();
}
}
}
}
}
SqlDataAdapter
代码如下:
/// <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 m = conn.CreateCommand();
m.CommandTimeout = _CommandTimeOut;
m.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter(m);
SqlCommandBuilder mandBulider = new SqlCommandBuilder(adapter);
mandBulider.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();
}
}
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程