C# 批量插入資料方法

來源:互聯網
上載者:User

標籤:ppi   efault   構造   資料庫   資料   oid   UI   guid   create   

 

批量插入資料方法

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();
}

C# 批量插入資料方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.