String conversion bug causes SqlBulkCopy to fail.

.net sql sqlbulkcopy


I have a view which generates a number of columns which I want to bulk load into another table which has identically named columns.

This procedure worked fine when I was looping over the SqlDataReader and doing an insert with SqlParameters using a SqlCommand each time. Obviously for many 100000s of rows, this was too slow.

I switched to using SqlBulkCopy, as it seemed this would work since the views column names and the target db tables column names and types match (since the above procedure worked!).

However, on the first record it fails with an InvalidOperationException saying "The given value of type String cannot be converted to type smalldatetime of the specified target column". There is just one date column in this first row and its value is NULL.

The view is only returning a subset of columns, but the ones it is not are all nullable.

Any help would be appreciated.

12/11/2009 10:35:19 AM

Accepted Answer

sqlbulkcopy does not automatically map between columns with the same name. It assumes that your select will return columns in the same order it sees them in the destination table.

So if there are 7 columns in the destination table and the view returns 5 it will try and put those 5 in the the first 5 columns of the destination table.

The SqlBulkCopy class can take a collection of column mappings. That should do the trick

12/11/2009 10:48:11 AM

Popular Answer

try setting the SET DATEFORMAT to correct format (ymd, dmy, mdy, ...) for your connection and retry. this error usually happens if you have dates in a localized format and not locale independent format.

as for doing it all in one insert this is not recommended because this is considered a long running transaction and not a bulk insert operation.

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