最近项目需要做一个批量导入的功能,每次导入最少的记录数都达到1万,之前写了一个通过循环Insert的方法,结果我自己都看不下了。太慢了,所以用了SqlBulkCopy,很快,100万条数据,1分钟都不到。
class Program { /// <summary> /// 批量插入 /// </summary> /// <param name="dt">数据集</param> public static void BulkToDB(DataTable dt) { using (SqlConnection sqlConn = new SqlConnection("Data Source=.;uid=sa;pwd=123456;Initial Catalog=Test")) { SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn); bulkCopy.DestinationTableName = "UserInfo"; //要插入数据的表名 bulkCopy.BatchSize = dt.Rows.Count; //插入的条数 try { sqlConn.Open(); if (dt != null && dt.Rows.Count != 0) bulkCopy.WriteToServer(dt); } catch (Exception ex) { throw ex; } finally { sqlConn.Close(); if (bulkCopy != null) bulkCopy.Close(); } } } /// <summary> /// 创建表结构,(Id(自增),LoginName,LoginPwd) /// </summary> /// <returns></returns> public static DataTable GetTableSchema() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[]{ new DataColumn("Id",typeof(int)), new DataColumn("LoginName",typeof(string)), new DataColumn("LoginPwd",typeof(string)) }); return dt; } static void Main(string[] args) { //100万条数据 Stopwatch sw = new Stopwatch(); for (int multiply = 0; multiply < 10; multiply++) { DataTable dt = GetTableSchema(); for (int count = multiply * 100000; count < (multiply + 1) * 100000; count++) { DataRow r = dt.NewRow(); // r[0] = 1;//因为该列是自增列,所以可以不赋值 r[1] = string.Format("Name-{0}", count * multiply); r[2] = string.Format("Pwd-{0}", count * multiply); dt.Rows.Add(r); } sw.Start(); BulkToDB(dt); sw.Stop(); Console.WriteLine(string.Format("Elapsed Time is {0} Milliseconds", sw.ElapsedMilliseconds)); } Console.ReadLine(); } }
执行结果: