批量插入数据方法
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();
}