In this case, the time for issuing the ticket will be 10 minutes-> 20 seconds. This is amazing.
Now, let's go to the demo, test, and change it to a method test that we generally use. NND is really fast.
Paste my Demo: SqlBulkCopy.rar here
Copy codeThe Code is as follows:
Using System;
Using System. Diagnostics;
Using System. Data;
Using System. Data. SqlClient;
Using Microsoft. ApplicationBlocks. Data;
Namespace ConsoleAppInsertTest
{
Class Program
{
Static int count = 1000000; // Number of inserted items
Static void Main (string [] args)
{
Long sqlBulkCopyInsertRunTime = SqlBulkCopyInsert ();
Console. WriteLine (string. Format ("the time used to insert {1} pieces of data using SqlBulkCopy is {0} millisecond", sqlBulkCopyInsertRunTime, count ));
Long commonInsertRunTime = CommonInsert ();
Console. WriteLine (string. Format ("the time taken to insert {1} pieces of data in normal mode is {0} millisecond", commonInsertRunTime, count ));
Console. ReadKey ();
}
/// <Summary>
/// Insert data normally
/// </Summary>
/// <Returns> </returns>
Private static long CommonInsert ()
{
Stopwatch stopwatch = new Stopwatch ();
Stopwatch. Start ();
For (int I = 0; I <count; I ++)
{
SqlHelper. ExecuteNonQuery (SqlHelper. SqlConnection, CommandType. Text, "insert into passport (PassportKey) values ('" + Guid. NewGuid () + "')");
}
Stopwatch. Stop ();
Return stopwatch. ElapsedMilliseconds;
}
/// <Summary>
/// Use SqlBulkCopy to insert data
/// </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); // The data initialization time.
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;
}
Private static DataTable GetTableSchema ()
{
Return SqlHelper. ExecuteDataset (SqlHelper. SqlConnection, CommandType. Text, "select * from Passport where 1 = 2"). Tables [0];
}
}
}