SqlBulkCopy the given value of String type from the data source cannot be converted to the type of the specified destination column bit

Source: Internet
Author: User
Tags bulk insert

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.