SqlBulkCopy 的缺点是什么
我对“使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到的两个原因:
insert
操作,而是偶尔执行insert< /code> 与
select
和update
混合在一起。 Microsoft 自己声明,在SqlBulkCopy
MSDN 页面。请注意,如果您希望 SqlBulkCopy 与普通插入等效,那么至少您必须向其传递 SqlBulkCopyOptions.CheckConstraints 参数。
Two reasons I can think of:
insert
s, but occasionalinsert
s intermixed withselect
s andupdate
s. Microsoft themselves state that a normalinsert
is more efficient for that, on theSqlBulkCopy
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 theSqlBulkCopyOptions.CheckConstraints
parameter.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.