"CreateTime"); sqlBulkcopy.WriteToServer(dt); } watch.Stop(

时间:2022-01-10 07:46:55

批量插入数据要领

void InsertTwo(List<CourseArrangeInfo> dtF)
{
Stopwatch watch = new Stopwatch();
watch.Start();
DataTable dt = new DataTable();//筹备存放数据的姑且表
//结构表的列
dt.Columns.Add("ShoppingCartID");
dt.Columns.Add("Produce_ID");
dt.Columns.Add("Number");
dt.Columns.Add("Customer_ID");
dt.Columns.Add("CreateTime");
foreach (var item in dtF)
{
DataRow rows = dt.NewRow();
//rows["ShoppingCartID"] = System.Guid.NewGuid().ToString();
//rows["Produce_ID"] = item.Produce_ID;
//rows["Number"] = item.Number;
//rows["Customer_ID"] = item.Customer_ID;
//rows["CreateTime"] = DateTime.Now;
//dt.Rows.Add(rows);
}
//重点代码措置惩罚惩罚方法
var ss = ConfigurationManager.ConnectionStrings["default"].ConnectionString;
string connStr = CEncrypt.DecryString(ss);//连接字符串
using (SqlBulkCopy sqlBulkcopy = new SqlBulkCopy(connStr))
{
sqlBulkcopy.DestinationTableName = "ShoppingCart";//界说需要存入数据库中的相应的表的名称
sqlBulkcopy.ColumnMappings.Add("ShoppingCartID", "ShoppingCartID");
sqlBulkcopy.ColumnMappings.Add("Produce_ID", "Produce_ID");
sqlBulkcopy.ColumnMappings.Add("Number", "Number");
sqlBulkcopy.ColumnMappings.Add("Customer_ID", "Customer_ID");
sqlBulkcopy.ColumnMappings.Add("CreateTime", "CreateTime");
sqlBulkcopy.WriteToServer(dt);
}
watch.Stop();
}