/*_____________________ 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)