复制代码 代码如下: 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_data
复制代码 代码如下: 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(); 注解: <1> sqlBulkCopy.DestinationTableName = "large"; large 指的是目标表的名称 <2> DataTable 的结构要和数据库中的表的结构相同 (DataTable的列不能多于数据库里面的) (DataTable的列可以少于数据库里面的 如果数据库这一列有默认值的话) <3> 这里面 我使用了事务 您在使用的时候 也可以不用事务