SQL中的批量处理
/// <summary>
/// 使用新的API,批量导入,速度快,这种方式只写一次日志,不会为每一行写日志
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btBCP_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(GetConnectionString()))
{
SqlBulkCopy bcp = new SqlBulkCopy(conn);
bcp.DestinationTableName = "Customers";
bcp.BatchSize = 100;//这是批尺寸可以调整
for (int i = 0; i < 11; i++)
{
bcp.ColumnMappings.Add(i, i);
}
conn.Open();
bcp.WriteToServer(ds.Tables[0]);
}
}
一切总是平衡的,这里少,那里补上!