使用 SqlBulkCopy 进行错误处理 - 还会更难吗?

发布于 2024-09-12 01:25:05 字数 389 浏览 3 评论 0原文

这里的想法非常少。我有一个案例,我使用 SqlBulkCopy 将数据泵入数据库,大约在一半的时候我遇到了不同的异常(主键违规、索引违规等)。

我已确认这些违规行为确实存在,并且需要在数据中进行更正。但令人恼火的是,如果我使用 DataAdapter 写入数据库(这会慢得多),DataSet 中的坏行将打开 HasErrors,这样我就可以轻松找到它们并处理问题。使用 SqlBulkCopy?齐尔奇。没什么。祝你好运,找到引起你​​问题的人,因为你得到的只是一个错误名称(比如“yada yada yada 中的主键违规,等等等等”),仅此而已。

有什么建议吗?我不敢相信没有办法得到这些错误。使用标准 BCP,我认为您甚至可以将这些内容写入日志文件。我们不能用 SqlBulkCopy 做这样的事情吗?

谢谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捶死心动 2024-09-19 01:25:05

查看 SqlBulkCopy 的源代码 - 您会在那里得到答案 - 那里没有真正的错误处理。

你可以做的一件事是运行一个错误的批处理,并将批处理大小设置为 1,尽管你必须这样做很蹩脚。

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.

晨敛清荷 2024-09-19 01:25:05

当我进行需要验证的数据导入时,我通常会将数据转储到一个表中,该表将按原样获取数据,然后运行存储过程或其他一些可以以基于集合的方式验证数据的 SQL,进行转换,并以我可以控制的方式将其放入最终目的地。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文