Bulk INSERT data with SqlBulkCopy implementation

Source: Internet
Author: User

1. Set up a test table testing

CREATE TABLE Test (f_name NVARCHAR () null, f_age INT null) 2, SqlBulkCopy BULK INSERT Data
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Text;4 usingSystem.Data.SqlClient;5 usingSystem.Data;6 7 namespaceSqlBulkCopy8 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -DataTable dt =NewDataTable (); -Dt. Columns.Add ("name",typeof(string)); theDt. Columns.Add (" Age",typeof(int)); -  -DataRow dr =dt. NewRow (); -dr["name"] ="Li thought"; +dr[" Age"] = -; - dt. Rows.Add (DR); +  ADr =dt. NewRow (); atdr["name"] ="Chen Qi"; -dr[" Age"] = -; - dt. Rows.Add (DR); -  -             using(System.Data.SqlClient.SqlConnection conn =NewSystem.Data.SqlClient.SqlConnection ("Database connection String")) -             { in Conn. Open (); -                 using(System.Data.SqlClient.SqlBulkCopy bulk =NewSystem.Data.SqlClient.SqlBulkCopy (conn)) to                 { +Bulk. DestinationTableName ="Test";//the name of the data table to import -Bulk. batchsize = dt. Rows.Count;//number of rows in each batch theBulk. Columnmappings.add ("name","F_name");//To set the mapping between columns in the data source and columns in the target table *Bulk. Columnmappings.add (" Age","F_age"); $ Bulk. WriteToServer (DT);Panax Notoginseng                     if(Bulk! =NULL) -                     { the Bulk. Close (); +                     } A                 } the Conn. Close (); +             } -         } $     } $}

  

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.