SqlBulkCopy 的缺点是什么

发布于 2024-07-24 08:49:52 字数 184 浏览 7 评论 0原文

我对“使用 C# 将大量数据插入数据库的最简单方法”做了一些研究,然后很多人建议我使用 SqlBulkCopy。 当我尝试之后,它确实让我惊讶。 毫无疑问,SqlBulkCopy 非常非常快。 看起来SqlBulkCopy是一种完美的插入数据(尤其是海量数据)的方式。 但我们为什么不一直使用它呢? 使用 SqlBulkCopy 有什么缺点吗?

I have done some research for "The bast way to insert huge data into DB with C#" then a lot of people just suggested me using SqlBulkCopy. After I tried it out and it really amazed me. Undoubtedly, SqlBulkCopy is very very fast. It seems that SqlBulkCopy is a perfect way to insert data (especially huge data). But why dont we use it at all times. Is there any drawback of using SqlBulkCopy?

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

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

发布评论

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

评论(2

许仙没带伞 2024-07-31 08:49:53

我能想到的两个原因:

  1. 据我所知,它仅适用于 Microsoft SQL Server
  2. 在许多正常工作负载中,您不会执行批量 insert 操作,而是偶尔执行 insert< /code> 与 selectupdate 混合在一起。 Microsoft 自己声明,在 SqlBulkCopy MSDN 页面

请注意,如果您希望 SqlBulkCopy 与普通插入等效,那么至少您必须向其传递 SqlBulkCopyOptions.CheckConstraints 参数。

Two reasons I can think of:

  1. As far as I know, it's only available for Microsoft SQL Server
  2. In a lot of normal workloads, you don't do bulk inserts, but occasional inserts intermixed with selects and updates. Microsoft themselves state that a normal insert is more efficient for that, on the SqlBulkCopy MSDN page.

Note that if you want a SqlBulkCopy to be equivalent to a normal insert, at the very least you'll have to pass it the SqlBulkCopyOptions.CheckConstraints parameter.

七秒鱼° 2024-07-31 08:49:52

Oracle v11 中也存在 SqlBulkCopy,但它是由安装 Oracle 客户端时获得的 Oracle .NET 程序集提供的。 SqlBulkCopy 类基本上是由目标数据库引擎的提供者一一实现的。

但有一个巨大的缺点——绝对没有错误报告。 例如,如果您更新了 DataSet 中的数据,并使用适配器将其刷新回数据库,并且存在关键违规(或任何其他故障),则罪魁祸首 DataRows 会将 .HasErrors 设置为 true,您可以当异常消息出现时,将其添加到异常消息中。

使用 SqlBulkCopy,您只需获取错误的类型即可。 祝调试顺利。

SqlBulkCopy does exist for Oracle v11 as well, but it's provided by the Oracle .NET assemblies you get when you install Oracle Client. The SqlBulkCopy class is basically implemented one by one, by the provider of the target database engine.

One HUGE drawback, though - there is absolutely no error reporting. If, for example, you've updated data in a DataSet, are flushing it back tothe DB with an adapter, and there's a key violation (or any other failure), the culprit DataRows will have .HasErrors set to true, and you can add that to your exception message when it's raised.

With SqlBulkCopy, you just get the type of the error and that's it. Good luck debugging it.

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