using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace winform测试插入 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string ConnectionString = string.Format("Data Source={0};database={1};uid=sa;pwd=123456", "192.168.200.101", "BulkTestDB"); string erro = string.Empty; private void button1_Click(object sender, EventArgs e) { var t = MSSQLHelper.TestConnection(out erro, ConnectionString); // inset_1000_data(); 14秒 bulk_1000_data(); ////111.8871 总毫秒数 } public voidbulk_1000_data () { // conditions must be constructed datatable @ var dt = 0 SELECT * from Top B_ProductCode; // the same structure of the query // if the key is inserted into the main data 5-10; inserting a second primary key 9 -11; 9-11 will not be inserted into the data; the DataTable dt = new new the DataTable (); dt.Columns.AddRange ( new new the DataColumn [] { new new the DataColumn ( " Id " , typeof ( int )), new new the DataColumn ( " UserName " , typeof (string)), //new DataColumn("Pwd",typeof(string)) }); for (int i = 0; i < 1000; i++) { DataRow r = dt.NewRow(); r[0] = i; r[1] = string.Format("User-{0}", i); // r[2] = string.Format("Pwd-{0}", i); dt.Rows.Add(r); } double test_time; // total number of data 5,553,345; five million data System.Diagnostics.Stopwatch Watch = new new System.Diagnostics.Stopwatch (); watch.Start (); // start monitoring time code to run // ----- ------------------------------------ // | // | BulkToDB (dt); // | // | // ------------------------------------------- Watch. sTOP (); // stop monitoring the TimeSpan TimeSpan = watch.Elapsed; // Get the total time of the current instance of the derived measuring test_timeTimespan.TotalMilliseconds =; // total number of milliseconds MessageBox.Show (test_time.ToString ()); // 111.8871 total number of milliseconds } public void inset_1000_data () { Double test_time; // total number of data 5,553,345; five million data System.Diagnostics.Stopwatch Watch = new new System.Diagnostics.Stopwatch (); watch.Start (); // start monitoring time code to run // ------------------ ----------------------- // | // | for ( int i = 0; i < 1000; i++) { StringBuilder sb = new StringBuilder(); sb.AppendFormat(" USE [BulkTestDB] "); sb.AppendFormat(" INSERT INTO [dbo].[BulkTestTable] "); sb.AppendFormat(" ([Id] "); sb.AppendFormat(" ,[UserName] "); sb.AppendFormat(" ,[Pwd]) "); sb.AppendFormat(" VALUES "); sb.AppendFormat(" ({0} ", i); sb.AppendFormat(" ,'name{0}'", i); sb.AppendFormat(" ,'pwd{0}')", i); MSSQLHelper.ExecuteNonQuery(sb.ToString(), out erro, ConnectionString); if (erro != string.Empty) { MessageBox.Show(erro); break; } } // | // | // ----------------------------------------- - watch.Stop (); // stop monitoring the TimeSpan TimeSpan = watch.Elapsed; // Get the total time of the current instance of the measurement results test_time = timespan.TotalMilliseconds; // total number of milliseconds MessageBox.Show (test_time.ToString () ); // 14659.1165 total number of milliseconds } // use Bulk insert [faster] #region [inserted usage Bulk] void BulkToDB (the DataTable dt) { the Stopwatch SW = new new the Stopwatch (); the SqlConnection sqlConn =new SqlConnection(ConnectionString); SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlconn); bulkCopy.DestinationTableName = "BulkTestTable"; //表名 bulkCopy.BatchSize = dt.Rows.Count; //数据量 try { sqlconn.Open(); if (dt != null && dt.Rows.Count != 0) { bulkCopy.WriteToServer(dt); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { sqlconn.Close(); if (bulkCopy != null) { bulkCopy.Close(); } } } #endregion } }
Transfer HTTPS: // www.cnblogs.com/zoro-zero/p/7743164.html