asp.net use sqlbulkcopy to insert data into SQL Server

Source: Internet
Author: User
Tags format bulk insert count insert sql net return string
In this way, our Fashui ticket time will be 10 minutes-->20 seconds, which is amazing.
As a then, the next demo, testing, change to their own general methods of testing, NND, can really say is the speed.
Put my Demo:SqlBulkCopy.rar in 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 bars inserted
static void Main (string[] args)
{
Long sqlbulkcopyinsertruntime = Sqlbulkcopyinsert ();
Console.WriteLine (String. Format ("The time used to insert {1} data using SqlBulkCopy is {0} milliseconds", Sqlbulkcopyinsertruntime, Count));
Long commoninsertruntime = Commoninsert ();
Console.WriteLine (String. Format (the time spent in the normal way to insert {1} data is {0} milliseconds, Commoninsertruntime, count));
Console.readkey ();
}
<summary>
Use normal Insert Data
</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>
Inserting data using the SqlBulkCopy method
</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);//Initializing Data 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];
}
}
}

From cnblogs article SQL BULK INSERT data performance Detailed comparison of several scenarios

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.