Copy codeThe Code is 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 indicates the name of the target table.
<2> the DataTable structure must be the same as that of the table in the database.
(DataTable cannot contain more columns than those in the database)
(The able column can be smaller than the column in the database if the column in the database has the default value)
<3> here I use transactions. You can also use them without transactions.