2013-09-24 35 views
1

我们有一些依赖于.NET SqlBulkCopy API的关键代码。有时,此代码将失败并出现以下错误:SqlServer BCP是全是还是全无?

System.Data.SqlClient.SqlException:由于目标表的模式更改而导致插入批量失败。

This blog post表明这是一个短暂的,几乎不可预知的错误。我想知道的是,当这个错误发生时,它确保没有行被插入(如果是这样,我可以简单地捕获这个异常并重试.NET端的BCP操作)?

我使用SqlServer的2008年

回答

0

你应该使用SqlBulkCopy Constructor (SqlConnection, SqlBulkCopyOptions, SqlTransaction)构造函数,它接受一个参数SqlTransaction。在SqlTransaction的情况下,批量复制将执行操作,如果发生故障,您可以选择回滚。

您还可以看到:Transaction and Bulk Copy Operations

Bulk copy operations can be performed as isolated operations or as part of a multiple step transaction. This latter option enables you to perform more than one bulk copy operation within the same transaction, as well as perform other database operations (such as inserts, updates, and deletes) while still being able to commit or roll back the entire transaction.

+0

将在TransactionScope类的工作呢?另外,是否将SqlBulkCopy放入交易会损害性能? – ChaseMedallion

+0

交易是为了数据完整性。它可能会影响大数据集的性能,也可能影响较小数据集的性能。你必须自己尝试一下,但IMO会影响性能,插入时间应该增加。 – Habib

相关问题