赞
踩
在一些大量数据导入数据库的情况下使用,性能提高很大,需要注意几点
一:方法的参数 是一个datatable。这个表的字段结构必须要实际库中表的结构一致,否则添加失败。
二:需要指定表名。tablename就是表名
/// <summary>
/// 批量插入文件流水数据
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public int BulkCopyDataToDB(DataTable dtItems)
{
try
{
string tableName = "LV_OrderList";
//插入原始数据到数据库中
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["YekCMS"].ToString()))
{
bulkCopy.DestinationTableName = tableName;//要插入的表的表名
bulkCopy.ColumnMappings.Add("orderId", "[orderId]");
if (dtItems.Columns.Contains("arg1"))
{
bulkCopy.ColumnMappings.Add("arg1", "[arg1]");
}
if (dtItems.Columns.Contains("arg2"))
{
bulkCopy.ColumnMappings.Add("arg2", "[arg2]");
}
bulkCopy.ColumnMappings.Add("categoryName", "[categoryName]");
bulkCopy.ColumnMappings.Add("productId", "[productId]");
bulkCopy.ColumnMappings.Add("productName", "[productName]");
bulkCopy.ColumnMappings.Add("oughtAmount", "[oughtAmount]");
bulkCopy.ColumnMappings.Add("paymentStatus", "[paymentStatus]");
bulkCopy.ColumnMappings.Add("productType", "[productType]");
bulkCopy.ColumnMappings.Add("buName", "[buCode]");
bulkCopy.ColumnMappings.Add("orderStatus", "[orderStatus]");
bulkCopy.ColumnMappings.Add("mediaId", "[mediaId]");
bulkCopy.ColumnMappings.Add("comm_type", "[comm_type]");
bulkCopy.ColumnMappings.Add("comm_status", "[comm_status]");
bulkCopy.ColumnMappings.Add("comm_rate", "[comm_rate]");
bulkCopy.ColumnMappings.Add("commission", "[commission]");
bulkCopy.ColumnMappings.Add("order_amout", "[order_amout]");
bulkCopy.ColumnMappings.Add("balance_status", "[balance_status]");
bulkCopy.ColumnMappings.Add("distribution_source", "[distribution_source]");
bulkCopy.ColumnMappings.Add("createDate", "[createDate]");
bulkCopy.BulkCopyTimeout = 600;
bulkCopy.BatchSize = 10000;
bulkCopy.WriteToServer(dtItems);
}
return 1;
}
catch
{
return 0;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。