C # insert big data into SQLServer

Source: Internet
Author: User
I used to insert big data one by one. Because the computer configuration was not good, it took me half an hour to insert 0.17 million data records. That hurts! The computer configured with 0.25 million pieces of data is completed in a few seconds. What is the magic code? I spent a lot of money to show off the Buddha.

I used to insert big data one by one. Because the computer configuration was not good, it took me half an hour to insert 0.17 million data records. That hurts! The computer configured with 0.25 million pieces of data is completed in a few seconds. What is the magic code? I spent a lot of money to show off the Buddha.

PreviousInsertLargeDataIt's always one by one.Insert, Because the computer configuration is not good, one 0.17 millionDataIt took about half an hour.InsertFinished. It hurts!

0.25 millionDataThe computer has been configured in a few seconds. What is the magic code?

I spent a lot of money to show off the Buddha.

  DataThe T_Nums table has four fields:

NumSection, AddressStr, NumType, AreaCode

Then read the input data from the text file to a able, and thenInsertDataLibrary speed is too fast

Run:

Because the code is relatively simple and does not write comments, you may not like reading help documents or leaving a message if you do not understand it...

The following code is used:

String connStr = "Data Source = WADE-PC; Initial Catalog = Test; Persist Security Info = True; User ID = sa; Password = 123456 ";

DateTime startTime = DateTime. Now;

OpenFileDialog dialog = new OpenFileDialog ();

Dialog. Filter = "text file (*. txt) | *. txt ";

If (dialog. ShowDialog () = DialogResult. OK)

{

DataTable dt = new DataTable ();

Dt. Columns. Add ("NumSection ");

Dt. Columns. Add ("AddressStr ");

Dt. Columns. Add ("NumType ");

Dt. Columns. Add ("AreaCode ");

IEnumerable lines = File. ReadLines (dialog. FileName, Encoding. Default );

Foreach (string str in lines)

{

String [] strs = str. Split ('\ t ');

DataRow row = dt. NewRow ();

Row ["NumSection"] = strs [0];

Row ["AddressStr"] = strs [1]. ToString (). Trim ('"');

Row ["NumType"] = strs [2]. ToString (). Trim ('"');

Row ["AreaCode"] = strs [3]. ToString (). Trim ('"');

Dt. Rows. Add (row );

}

Using (SqlBulkCopy bulkCopy = new SqlBulkCopy (connStr ))

{

BulkCopy. DestinationTableName = "T_Nums ";

BulkCopy. ColumnMappings. Add ("NumSection", "NumSection ");

BulkCopy. ColumnMappings. Add ("AddressStr", "AddressStr ");

BulkCopy. ColumnMappings. Add ("NumType", "NumType ");

BulkCopy. ColumnMappings. Add ("AreaCode", "AreaCode ");

BulkCopy. WriteToServer (dt );

}

TimeSpan ts = DateTime. Now. Subtract (startTime );

MessageBox. Show ("TotalInsert"+ Dt. Rows. Count +"Data, Time: "+ ts. ToString ());

[Editor: peter]

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.