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.