English 中文(简体)
SqlBulkCopy failing with String conversion issue
原标题:

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.

最佳回答

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

问题回答

Why do you nout just use a single sql statement

INSERT INTO YourTable (Col1, Col2,...) SELECT Col1, Col2, .. FROM YourView

If the view has the same columns as the table, you can drop the (Col1, Col2,...) after YourTable and change the statement to

INSERT INTO YourTable SELECT Col1, Col2, .. FROM YourView

But in general it is good form to specify the columns you are inserting into.

You can then also apply a where clause to the select from YourView.

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.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签