C # data is bulk inserted into the database SqlBulkCopy (source data type:list<t> Or DataTable)

Source: Internet
Author: User
Tags bulk insert

/*_____________________ list<t> type data to sql_______________________________*/

<summary>
Sqlbulkcopies the specified SMS. BULK INSERT INTO Database
</summary>
<param name= "Data" >list datatype .</param>
<param name= "sqlconn" > Database connection string .</param>
private void Sqlbulkcopy (list<t> data, SqlConnection sqlconn)
{
#region processing of pending data initialization
list<propertyinfo> pList = new list<propertyinfo> ();//Create a collection of properties
DataTable dt = new DataTable ();
Add all the public attributes to the collection and the columns of the DataTable
Array.foreach<propertyinfo> (typeof (T). GetProperties (), p = = {Plist.add (p); dt. Columns.Add (P.name, P.propertytype);  }); Gets the reflected entrance (typeof ())//System.Action to be performed on each element of the array.
foreach (var item in SMS)
{
DataRow row = dt. NewRow (); Create a DataRow instance
Plist.foreach (p = row[p.name] = p.getvalue (item, NULL)); Assign a value to row
Dt. Rows.Add (row); Add to DataTable
}
#endregion
#region BULK INSERT Database SqlBulkCopy declaration and parameter settings
SqlBulkCopy bulk = new SqlBulkCopy (sqlconn. ToString (), sqlbulkcopyoptions.useinternaltransaction) {destinationtablename = "TableName"/* Set database target table name */, BatchSize = dt. Rows.Count/* Number of lines in each batch */};
Bulk. Columnmappings.add ("id", "id"); To set the mapping between columns in the data source and columns in the target table
Bulk. Columnmappings.add ("name", "name"),//columnmappings.add ("Source data table column name", "Target table data column name");
Bulk. Columnmappings.add ("Sex", "sex");
Bulk. Columnmappings.add ("Phone", "phone");

.

.

.
#endregion
Bulk. WriteToServer (DT);
if (bulk! = NULL)
{
Bulk. Close ();
}
}

/*_____________________ DataTable type data to sql_______________________________*/

<summary>
Sqlbulkcopies the specified SMS. BULK INSERT INTO Database
</summary>
<param name= "DT" >datatable type with insert data </param>
<param name= "sqlconn" > Database connection string </param>
private void Sqlbulkcopy (DataTable dt, SqlConnection sqlconn)
{
#region BULK INSERT Database SqlBulkCopy declaration and parameter settings
SqlBulkCopy bulk = new SqlBulkCopy (sqlconn. ToString (), sqlbulkcopyoptions.useinternaltransaction) {destinationtablename = "eng_failsendsms"/* Set database target table name */, batchsize = dt. Rows.Count/* Number of lines in each batch */};
Bulk. Columnmappings.add ("id", "id"); To set the mapping between columns in the data source and columns in the target table
Bulk. Columnmappings.add ("name", "name"),//columnmappings.add ("Source data table column name", "Target table data column name");
Bulk. Columnmappings.add ("Sex", "sex");
Bulk. Columnmappings.add ("Phone", "phone");

.

.

.

#endregion
Bulk. WriteToServer (DT);
if (bulk! = NULL)
{
Bulk. Close ();
}
}

C # data is bulk inserted into the database SqlBulkCopy (source data type:list<t> Or DataTable)

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.