Byte with SqlBulkCopy[] Error in DataTable column

bytearray c# sqlbulkcopy strongly-typed-dataset

Question

I'm attempting to insert a highly typed dataset field from a datatable having a byte[] column as one of its columns into a binary(4) database table field. I have no issues setting the values for the byte[] column, but when I use sqlbulkcopy on the datatable, I get the following exception:

"The supplied type Int32 value from the data source cannot be translated to type binary of the chosen destination column," the error message states.

The database table and the big datatable both function well using sqlbulkcopy, with the exception of the byte[]/binary(4) columns. The code that I introduced to break SqlBulkCopy using.NET 2.0 is as follows.

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

...

using(SqlBulkCopy bc = new SqlBulkCopy(conn))
{
    bc.DestinationTableName = dt.TableName;
    bc.WriteToServer(dt);
    bc.Close();
}
1
0
7/27/2010 9:31:21 PM

Accepted Answer

I identified the issue. My datatable's Byte[] column was not in the same ordinal spot as the related database column. Since the database column ordinal was 8 and the datatable column ordinal was 56, either the datatable or the columnmapping for the sqlbulkcopy needed to be reorganized. The datatable was considerably faster and easy to reorganize.

1
7/28/2010 12:50:52 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow