6
在數據庫中插入記錄時出現錯誤。Sql事務失敗
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
如果有人能幫助我解決這個問題,我會很感激。
你使用了什麼連接超時? –
嘗試將您的代碼放入try-catch塊並檢查調試器中的異常。我已經完成了這個工作,有時會在這個問題上得到更多的細節,就像NULL被插入到一個不可爲空的字段中那樣。 – Melanie
如果您在沒有事務的情況下運行它,您的操作是否成功?如果是這樣,需要多長時間才能完成交易? – Nathan