完整代码示例:
- using System;
- using System.Data;
- using System.Configuration;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Diagnostics;
- using SubSonic;
- using UsersDAL;
- using System.Data.SqlClient;
- namespace UserManage.Code
- {
- public class SqlBulkCopy1
- {
- public static long SqlBulkCopyInsert(DataTable dt,string connectionString)
- {
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- DataTable dataTable = dt;
- string passportKey;
- for (int i = 0; i < 100000; i++)
- {
- passportKey = Guid.NewGuid().ToString();
- DataRow dataRow = dataTable.NewRow();
- dataRow = passportKey;
- dataRow = "aaaaaa";
- dataRow = "男";
- dataRow = "address";
- dataTable.Rows.Add(dataRow);
- }
- SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(connectionString);
- sqlBulkCopy.DestinationTableName = "Users";
- sqlBulkCopy.BatchSize = dataTable.Rows.Count;
- SqlConnection sqlConnection = new SqlConnection(connectionString);
- sqlConnection.Open();
- if (dataTable != null && dataTable.Rows.Count != 0)
- {
- sqlBulkCopy.WriteToServer(dataTable);
- }
- sqlBulkCopy.Close();
- sqlConnection.Close();
- return stopwatch.ElapsedMilliseconds;
- }
- }
- }