views:

40

answers:

2

Running very low on ideas here. I've got a case where I'm using SqlBulkCopy to pump data into a DB, and about halfway through I run into different exceptions (primary key violations, index violations, etc).

I've confirmed that the violations are in fact true and need to be corrected in the data. What's infuriating, though, is that if I were writing to the DB with a DataAdapter (which would be far slower), the bad rows in the DataSet would have HasErrors turned on so I could easily find them and take care of things. With SqlBulkCopy? Zilch. Nada. Good luck finding whoever caused your problem because all you'll get is an error name (like "primary key violation in yada yada yada, blah blah blah") and that's it.

Any suggestions? I can't believe there's no way to get these errors. With the standard BCP I think you can even pump these things to a log file. Can't we do something like this with SqlBulkCopy?

Thx,

A: 

Have a look at the source code for SqlBulkCopy - you'll get your answer there - there is no real error handling there.

One thing you could do, although lame that you would have to, is to run an erroring batch with the batch size set to 1.

Matt Whitfield
The source code for this class is available? Didn't think MS would put it out there.Could you elaborate a bit on the idea of the error batch? I'm following your train of thought...Thx!
David Catriel
Yeah, it's part of the standard .NET source code. Check out http://netmassdownloader.codeplex.com/ for the easiest way to get the source. With the error batch I mean for each row, clear the data table, call WriteToServer() - and you will then get a better idea of which row has failed. Performance would suck - but if you only did it for erroring batches then it may work out ok?
Matt Whitfield
A: 

When I'm doing data imports requiring validation, I usually will dump the data into a table that will take the data as is, then run a stored proc or some other sql that can validate my data in a set based manner, do the transformations, and put it into the final destination in a way that I can control.

Jeremy
This works, except that you're now duplicating all your data validation rules. You have them both on the table itself and in the stored proc. Double the maintenance.
David Catriel
Not true...validation in one place...the sproc. The bulk insert just gets it over the wire quickly where it can be dealt with in a set based way. It is either that or insert one at a time which can be dog slow.
Jeremy