SQL 服务器 2012 - 批量插入错误 - 此操作与此事务上的另一个挂起操作冲突

SQL Server 2012 - Bulk insert error - This operation conflicts with another pending operation on this transaction

我们正在使用 C# 组件执行批量插入操作。

这是代码:

using (SqlCommand sqlCommand = new SqlCommand("SET XACT_ABORT ON", _sqlConnection))
{
    sqlCommand.SafeExecuteNonQuery();
}

var sqlBulkCopy = new SqlBulkCopy(_sqlConnection, bulkCopyOptions, null);

sqlBulkCopy.WriteToServer(table);

出现以下错误:

This operation conflicts with another pending operation on this transaction. The operation failed.

堆栈跟踪:

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlBulkCopy.RunParser(BulkCopySimpleResultSet bulkCopyHandler)
at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinuedOnSuccess(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestAsync(CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalAsync(CancellationToken ctoken)
at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServerAsync(Int32 columnCount, CancellationToken ctoken)
at System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table, DataRowState rowState)

当我调试代码并跳过执行 SET XACT_ABORT ON 语句的代码时,它突然工作并继续工作。

当我恢复数据库并再次尝试时,我再次遇到错误。

批量插入只是在 table.

中插入一条记录

有谁知道是什么导致了这个问题?

批量复制选项中的 "SqlBulkCopyOptions.CheckConstraints" 是否可以解决问题?在我的例子中,table 我正在做批量复制以获得一个 table 的外键,我在同一个事务中插入一条记录。如果是这种情况,您可能希望批量复制 table 不检查转到另一个 table.

的外键中的现有记录