Asp.net uses SqlBulkCopy to insert data to SQL Server quickly

Source: Internet
Author: User

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];
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.