using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; namespace CHMExam { public partial class InsertDataTable : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataTable dtNorthWindOrders = new DataTable(); using (SqlConnection northWindConn = new SqlConnection(@"Data Source=WWW-90882E3DA97/SQLEXPRESS;Initial Catalog=test;User ID=sa; Password=test;")) { using (SqlDataAdapter northWindAda = new SqlDataAdapter("select * from Orders", northWindConn)) { northWindAda.Fill(dtNorthWindOrders); } northWindConn.Close(); } using (SqlConnection testOrdersConn = new SqlConnection(@"Data Source=WWW-90882E3DA97/SQLEXPRESS;Initial Catalog=test;User ID=sa; Password=test;")) { testOrdersConn.Open(); using (SqlTransaction tran = testOrdersConn.BeginTransaction()) { SqlBulkCopy bulkCopyOrders = new SqlBulkCopy(testOrdersConn, SqlBulkCopyOptions.Default, tran); bulkCopyOrders.DestinationTableName = "Test_Orders"; bulkCopyOrders.ColumnMappings.Add("CustomerID", "Test_CustomerID"); bulkCopyOrders.ColumnMappings.Add("OrderDate", "Test_OrderDate"); bulkCopyOrders.ColumnMappings.Add("ShipName", "Test_ShipName"); bulkCopyOrders.BulkCopyTimeout = 1000; bulkCopyOrders.SqlRowsCopied += new SqlRowsCopiedEventHandler(onRowsCopy); bulkCopyOrders.NotifyAfter = 10; try { bulkCopyOrders.WriteToServer(dtNorthWindOrders); tran.Commit(); } catch (Exception ex) { Response.Write(ex.ToString()); } finally { dtNorthWindOrders = null; testOrdersConn.Close(); } } } } private void onRowsCopy(object Sender, SqlRowsCopiedEventArgs args) { Response.Write("已复制:<font color=red>" + args.RowsCopied.ToString() + "</font><br/>"); } } } 具体操作在E:/Person/lianxi/CHMExam/CHMExam