Sql事务失败

时间:2013-04-18 20:51:57

标签: c# sql-server transactionscope

我在数据库中插入记录时遇到错误。

System.Transactions.TransactionException: The operation is not valid for the state of the transaction. ---> System.TimeoutException: Transaction Timeout
   --- End of inner exception stack trace ---
   at System.Transactions.TransactionState.EnlistPromotableSinglePhase(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction atomicTransaction)
   at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification)
   at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
   at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()

实际上我是通过事务范围方法在数据库中插入数据,其代码如下所述。

TransactionOptions tOptions = new TransactionOptions();
tOptions.IsolationLevel = IsolationLevel.ReadCommitted;
tOptions.Timeout = TransactionManager.MaximumTimeout;
using (var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, tOptions))
{
}

上述陈述中是否有任何不正确之处?

这里要提到的一点是,我在大量10个表中大量插入数据,包含大量记录和表,也允许在批量插入中插入重复记录,下面将提到用于实现此目的的语法。

CREATE UNIQUE INDEX Index_a ON table1([c1], [c2]) WITH IGNORE_DUP_KEY

如果有人能帮我解决这个问题,我将感激不尽。

2 个答案:

答案 0 :(得分:1)

问题是MSDTC服务被禁用,我正在使用批量数据库操作。启用后,问题得以解决。

答案 1 :(得分:0)

好像你有未提交的交易。尝试重新启动Sql Server或Oracle服务。