0

I have a strongly typed dataset containing a datatable with one of the columns as a byte[] column that I'm trying to insert into a binary(4) database table field. I'm able to set the byte[] column values without any problems, but I receive the following exception when I run sqlbulkcopy on the datatable:

"The given value of type Int32 from the data source cannot be converted to type binary of the specified target column."

The datatable is a large datatable and the sqlbulkcopy works fine with the datatable and database table minus the byte[]/binary(4) columns. The following is the code that I've inserted that is breaking SqlBulkCopy using .NET 2.0.

byte[] codeByteArray = GetByteArray();
dt.byteArrayCol = codeByteArray;

...

using(SqlBulkCopy bc = new SqlBulkCopy(conn))
{
    bc.DestinationTableName = dt.TableName;
    bc.WriteToServer(dt);
    bc.Close();
}
1
  • Could you show the creation of the DataTable? Jul 27, 2010 at 21:31

1 Answer 1

1

I discovered my problem. The Byte[] column in my datatable was not in the same ordinal position as the corresponding database column ordinal. The datatable column ordinal was 56 while the database column ordinal was 8, thus either the datatable needed to be re-organized or the columnmapping for the sqlbulkcopy. Reorganizing the datatable was much easier and quicker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.