Efficient code processing for millions of data stored in SqlBulkCopy

Source: Internet
Author: User

The most acute sentence is writetoserver (tablename ). This does not explain. 10000... remember that it takes more than four seconds for 1 million pieces of data to be written into the database... Note that the Field Format of the dataset to be written must be the same as that of the target data table in the database tutorial. After half a day, the table framework and content must be the same,... Writing data is a fast process ~ The disadvantage is the data judgment and filtering before writing, which means a lot of effort as it approaches easy. Let's look at the code below.

1 /// <summary>
2 // use sqlbulkcopy to import data from excel to SQL tables
3 /// </summary>
4 /// <param name = "tablename"> data source </param>
5 /// <param name = "destintable"> target table </param>
6 public void sqlbulkcopyinsert (datatable tablename, string destintable, sqlconnection conn, bool flag)
7 {
8 sqlbulkcopy sbc = new sqlbulkcopy (conn );
9
10 if (conn. state = connectionstate. closed)
11 {
12 conn. open ();
13}
14 sbc. destinationtablename = destintable; // obtain the target data table to be imported
15 try
16 {
17 sbc. writetoserver (tablename); // write
18}
19 catch (exception ex)
20 {
21 console. writeline (ex. message );
22}
23 finally
24 {
25 sbc. close ();
26 if (flag)
27 {
28 conn. close ();
29}
30}
31}

Many of my friends who write data into the database are familiar with sqlbulkcopy. I also happened to see this method in Microsoft's msdn document. When I first joined the company, there was an O & M project to be implemented. Because it was used by the company, the data volume would not be small. if I used the if statement first, then insert into to the database would be better, if there are tens of thousands of records, the page is not time-out, so we can use a powerful method to write data to the database.

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.