With SqlBulkCopy BULK INSERT, you can quickly insert large quantities and perform very well
"A given value from a String type from a data source cannot be converted to the type bit of the specified destination column" Exception when it is used
The DataTable is not the same as the field where you want to insert the data table.
DataTable with the data table to be inserted to the field name, location, data type is only available
example, using SqlBulkCopy to insert multiple tables
Public BOOLBatchinsertuniqecode (DataTable uniquecodeproducecontrastdt,datatable Uniquecodegeneratedt) {using(SqlConnection conn= (SqlConnection) (Base. DbContext.Database.Connection)) {if(Conn. state = =connectionstate.closed) {conn. Open (); } sqltransaction Tran=Conn. BeginTransaction (); SqlBulkCopy Upblock=NewSqlBulkCopy (conn, Sqlbulkcopyoptions.default, Tran); SqlBulkCopy Ugblock=NewSqlBulkCopy (conn, Sqlbulkcopyoptions.default, Tran); Try{ugblock.destinationtablename="uniquecodegenerate"; Ugblock.batchsize=UniqueCodeGenerateDt.Rows.Count; if(Uniquecodegeneratedt! =NULL&& UniqueCodeGenerateDt.Rows.Count >0) {ugblock.writetoserver (Uniquecodegeneratedt); } ugblock.close (); Upblock.destinationtablename="Uniquecodeproducecontrast"; Upblock.batchsize=UniqueCodeProduceContrastDt.Rows.Count; if(Uniquecodeproducecontrastdt! =NULL&& UniqueCodeProduceContrastDt.Rows.Count >0) {upblock.writetoserver (UNIQUECODEPRODUCECONTRASTDT); } upblock.close (); Tran.commit (); return true; } Catch(Exception ex) {tran. Rollback (); Throwex; } finally{upblock.close (); Ugblock.close (); Conn. Close (); } } }
SqlBulkCopy the given value of String type from the data source cannot be converted to the type of the specified destination column bit