Copy Code code as follows:
Using System.Data;
Using System.Diagnostics;
Using System.Data.SqlClient;
String connectionString = "Data source=hg-j3ejj9lsw5py;initial catalog=test; User ID=SA;PASSWORD=HG ";
DataTable datatable = Sql_.select_datagrid ("Select a from large where 1=0"). Tables[0];
String Passportkey;
for (int i = 0; i < 100000; i++)
{
Passportkey = Guid.NewGuid (). ToString ();
DataRow DataRow = Datatable.newrow ();
Datarow[0] = Passportkey;
DATATABLE.ROWS.ADD (DataRow);
}
SqlConnection SqlConnection = new SqlConnection (connectionString);
Sqlconnection.open ();
SqlTransaction Sqltran = SqlConnection.BeginTransaction ();
SqlBulkCopy sqlbulkcopy = new SqlBulkCopy (SqlConnection, Sqlbulkcopyoptions.keepidentity, Sqltran);
Sqlbulkcopy.destinationtablename = "large";
Sqlbulkcopy.batchsize = DataTable.Rows.Count;
if (dataTable!= null && dataTable.Rows.Count!= 0)
{
Sqlbulkcopy.writetoserver (dataTable);
}
Sqlbulkcopy.close ();
Sqltran. Rollback ();
Sqlconnection.close ();
Note: <1> sqlbulkcopy.destinationtablename = "large"; Large refers to the name of the target table
The structure of the <2> DataTable is the same as the structure of the tables in the database
(a DataTable cannot have more columns than a database)
(The DataTable column can be less than the database if the column has a default value)
<3> in which I used the transaction you can also use the time without the transaction