Big Data Bulk Insert database usage (SqlBulkCopy) more efficient

Source: Internet
Author: User
Tags bulk insert

SqlBulkCopy class is System.Data.SqlClient under the class, we are not used in development, and even do not know that there is such a class exists, but it is more than SQL INSERT, transaction BULK INSERT, SQL bulk splicing is much faster than the call stored procedure insert Convenient

Here's how to use it:

public static bool Executetransactionscopeinsert (DataTable DT, int batchsize)

{

  • int count = dt.  Rows.Count;
  • string tableName = "TestTable";
  • int copytimeout = 600;
  • BOOL flag = false;
  • Try
  • {
  • using (SqlConnection CN = New SqlConnection (connectionString))
  • {
  • using (TransactionScope scope = new TransactionScope ())
  • {
  • cn. Open ();
  • using (SqlBulkCopy SBC = New SqlBulkCopy (CN))
  • {
  • //Name of the target table on the server
  • Sbc. DestinationTableName = TableName;
  • Sbc. BatchSize = batchsize;
  • Sbc. Bulkcopytimeout = Copytimeout;
  • For (int i = 0; i < dt. Columns.count; i++)
  • {
  • //Column mappings define relationships between columns in the data source and columns in the target table
  • Sbc. Columnmappings.add (dt. Columns[i]. ColumnName, dt. Columns[i]. ColumnName);
  • }
  • Sbc. WriteToServer (DT);
  • Flag = true;
  • Scope.complete (); //Valid transactions
  • }
  • }
  • }
  • }
  • catch (Exception ex)
  • {
  • Loghelper.error (ex. Message);
  • return false;
  • }
  • return flag;
  • }  

SqlBulkCopy principle is the use of SQL Server BCP protocol for data bulk replication, combined with transactions, in our case, about each batch of 800 is the balance point, performance than insert increased by 100 times, And more than 7 times times more performance than the same case with transactional bulk inserts

Here is the link from the source author experiment: http://blog.csdn.net/amandag/article/details/6393717

Big Data Bulk Insert database usage (SqlBulkCopy) more efficient

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.