protected void button1_Click(object sender, EventArgs e)
{
DataTable dtSource = new DataTable();
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);
}
//将内存表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 + "毫秒";
}