Ado.net--sqlbulkcopy BULK INSERT Data

Source: Internet
Author: User
Tags bulk insert

In general, we use INSERT statements when inserting data into a database, but when the amount of data is large, this is a slow situation, and this is the time to sqlbulkcopy this class.

There are so few functions that are commonly used by SqlBulkCopy itself.

DestinationTableName-----The name on the server target table, which table you want to insert a large amount of data into, which is set to that table name

ColumnMappings-----Field mappings because you want to create a table locally and then add the entire table one time to the database, so you need to map the fields of the local table to the fields in the database table

WriteToServer-----This is to write to the database, do not need to say more

The imported data is the mobile phone number attribution information provided on the free video of the Wisdom podcast, inserted into the database to look at, a total of 258,113 data, the time is about 6 seconds.

Private void btndr_click (object sender, routedeventargs e)          {             Openfiledialog ofd = new openfiledialog ();             ofd. filter =  "text file |*.txt";             if (OFD . ShowDialog ()!=true)             {                 return;             }             datetime time = datetime.now;//time to see how long it took                           DatataBle table = new datatable ();//create a temporary table locally              //adding column names to temporary tables              Table. Columns.Add ("HD");             table. Columns.Add ("DQ");             table. Columns.Add ("LX");             using (StreamReader  streamreader=new streamreader (OFD. Filename,encoding.default)//Read file             {                 while (! Streamreader.endofstream)                  {                  &nbSp;  string[] stringstream = streamreader.readline (). Split (' \ t ');//read by line, split with \ t to get array                      string hd = stringstream[0]. Trim (' "');                     STRING DQ = STRINGSTREAM[1]. Trim (' "');                     STRING LX = STRINGSTREAM[2]. Trim (' "');                     //inserting data into a locally created temporary table                      datarow row = table. NewRow ();                     row["HD"] = hd;                     row["DQ"] =  DQ;                     row["LX"] = lx;                     table. Rows.Add (Row);                 }            }             //This part is imported from the local to the database              using (Sqlconnection conn=new sqlconnection ("Data source=.;i Nitial catalog=imageprocess;integrated security=true "))             {                 conn. Open ();                 using ( SQLBULKCOPY BULKCOPY=NEW SQLBULKCOPY (conn))                  {                     bulkCopy.DestinationTableName =  "T_hm";                      bulkcopy.columnmappings.add ("HD",  "HD");//The preceding parameter is the local temporary table column name, followed by the database column name. Two names do not need the same, I write the same, is for convenience                      bulkcopy.columnmappings.add ("dq",  "dq");       &nbSp;             bulkcopy.columnmappings.add (" LX ", " LX ");                     bulkcopy.writetoserver (table);                 }             }            timespan ts =  DateTime.Now - time;             MessageBox.Show (TS. ToString ());                    }

Ado.net--sqlbulkcopy BULK INSERT Data

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.