Sqlbulkcopy efficient database writing demo

Source: Internet
Author: User

I believe that anyone who has written a large amount of data into the database is 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 pages, the page is not time-out, so we can use a powerful method to write data to the database. Oh, it seems like a lot of nonsense is coming up ------

 

Code

     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 operation
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 }

ActuallyCodeVery simple. 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. 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 when it approaches easy. Let's just talk about it, hey...

 

 

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.