protected void button1_Click(object sender, EventArgs e)sql
{數據庫
DataTable dtSource = new DataTable();app
dtSource.Columns.Add("Name", typeof(string));ui
dtSource.Columns.Add("Address", typeof(string));this
DataRow dr;pwa
for (int i = 0; i < 100 * 100; i++)對象
{blog
dr = dtSource.NewRow();內存
dr["Name"] = "Name" + i;string
dr["Address"] = "Address" + i;
dtSource.Rows.Add(dr);
}
//將內存表dt中的1W條數據一次性插入到t_Data表中的相應列中
System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
st.Start();
string connStr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
using (SqlBulkCopy copy = new SqlBulkCopy(connStr))
{
//1 指定數據插入目標表名稱
copy.DestinationTableName = "Student";
//2 告訴SqlBulkCopy對象 內存表中的 OrderNO1和Userid1插入到OrderInfos表中的哪些列中
copy.ColumnMappings.Add("Name", "Name");
copy.ColumnMappings.Add("Address", "Address");
//3 將內存表dt中的數據一次性批量插入到OrderInfos表中
copy.WriteToServer(dtSource);
}
st.Stop();
this.lblPL.InnerText="數據插入成功,總耗時爲:" + st.ElapsedMilliseconds + "毫秒";
}
protected void button2_Click(object sender, EventArgs e)
{
DataTable dtSource = new DataTable();
dtSource.Columns.Add("ID", typeof(int));
dtSource.Columns.Add("Name", typeof(string));
dtSource.Columns.Add("Address", typeof(string));
DataRow dr;
for (int i = 0; i < 100 * 100; i++)
{
dr = dtSource.NewRow();
dr["Name"] = "Name" + i;
dr["Address"] = "Address" + i;
dtSource.Rows.Add(dr);
}
System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch(); //計算時間
st.Start();
string conn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; //鏈接數據庫
string sqlText = "select * from Student"; //SQL語句,用於查出符合條件的數據庫數據
//當上述工做完成以後,咱們調用SqlDataAdapter的Fill()方法,將查詢出來的數據表內容填充的一張DataTable裏面
SqlDataAdapter SDA = new SqlDataAdapter(sqlText, conn);
SDA.Fill(dtSource);
//這個SqlCommandBuilder用來自動生成添加、刪除、修改的語句,注意這個參數是剛纔創建的SqlDataAdapter。
SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
SDA.Update(dtSource); //數據錄入
st.Stop();
this.lblCT.InnerText="數據插入成功,總耗時爲:" + st.ElapsedMilliseconds + "毫秒";
}