/// <summary>
- /// 使用SqlBulkCopy方式插入数据
- /// </summary>
- /// <returns></returns>
- private static long SqlBulkCopyInsert()
- {
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- DataTable dataTable = GetTableSchema();
- for (int i = 0; i < count; i++)
- {
- DataRow dataRow = dataTable.NewRow();
- dataRow[2] = Guid.NewGuid();
- dataTable.Rows.Add(dataRow);
- }
- //Console.WriteLine(stopwatch.ElapsedMilliseconds);//初始化数据时间
- SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(SqlHelper.SqlConnection);
- sqlBulkCopy.DestinationTableName = "Passport";
- if (dataTable != null && dataTable.Rows.Count != 0)
- {
- sqlBulkCopy.WriteToServer(dataTable);
- }
- sqlBulkCopy.Close();
- stopwatch.Stop();
- return stopwatch.ElapsedMilliseconds;
- }