两种方法,直接上代码
方法一:
文件字段使用逗号分隔,行使用“|”分隔。
SqlProvider.ExecuteNonQuery("BULK INSERT Customer FROM 'c:\\100w.txt' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR='|',BATCHSIZE = 100000)");
方法二:
构建100000数据:
private void GetData()
{
try
{
dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("email", typeof(string));
dt.Columns.Add("address", typeof(string));
dt.Columns.Add("phone", typeof(string));
for (int i = 0; i < 1000000; i++)
{
DataRow dr = dt.NewRow();
dr["id"] = i;
dr["name"] = "name_" + i;
dr["email"] = "email_" + i;
dr["address"] = "address_" + i;
dr["phone"] = "phone_" + i;
dt.Rows.Add(dr);
}
}
finally
{
}
}
protected void F1()
{
SqlConnection conn = new SqlConnection(str);
conn.Open();
System.Diagnostics.Stopwatch timer = new Stopwatch();
using (System.Data.SqlClient.SqlBulkCopy sqlBC = new System.Data.SqlClient.SqlBulkCopy(conn))
{
sqlBC.BatchSize = 100000;
sqlBC.BulkCopyTimeout = 60;
sqlBC.Destinati;
sqlBC.ColumnMappings.Add("id", "customerId");
sqlBC.ColumnMappings.Add("name", "name");
sqlBC.ColumnMappings.Add("email", "email");
sqlBC.ColumnMappings.Add("address", "address");
sqlBC.ColumnMappings.Add("phone", "phone");
timer.Start();
sqlBC.WriteToServer(dt);
timer.Stop();
}
conn.Dispose();
MessageBox.Show(timer.ElapsedMilliseconds.ToString());
}
第二种方式插入100万数据,普通的pc 10秒钟左右。