asp.net code (SQL Server) that quickly inserts large amounts of data _mssql

Source: Internet
Author: User
Copy Code code as follows:

Using System.Data;
Using System.Diagnostics;
Using System.Data.SqlClient;
   
String connectionString = "Data source=hg-j3ejj9lsw5py;initial catalog=test; User ID=SA;PASSWORD=HG ";
DataTable datatable = Sql_.select_datagrid ("Select a from large where 1=0"). Tables[0];
String Passportkey;
for (int i = 0; i < 100000; i++)
{
Passportkey = Guid.NewGuid (). ToString ();
DataRow DataRow = Datatable.newrow ();
Datarow[0] = Passportkey;
DATATABLE.ROWS.ADD (DataRow);
}
SqlConnection SqlConnection = new SqlConnection (connectionString);
Sqlconnection.open ();
SqlTransaction Sqltran = SqlConnection.BeginTransaction ();
SqlBulkCopy sqlbulkcopy = new SqlBulkCopy (SqlConnection, Sqlbulkcopyoptions.keepidentity, Sqltran);
Sqlbulkcopy.destinationtablename = "large";
Sqlbulkcopy.batchsize = DataTable.Rows.Count;
if (dataTable!= null && dataTable.Rows.Count!= 0)
{
Sqlbulkcopy.writetoserver (dataTable);
}
Sqlbulkcopy.close ();
Sqltran. Rollback ();
Sqlconnection.close ();

Note: <1> sqlbulkcopy.destinationtablename = "large"; Large refers to the name of the target table
The structure of the <2> DataTable is the same as the structure of the tables in the database
(a DataTable cannot have more columns than a database)
(The DataTable column can be less than the database if the column has a default value)
<3> in which I used the transaction you can also use the time without the transaction
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.