我正在使用'TransactionScope',我需要在C#代码中做一些DML,我已经成功了。
我需要找出 交易的状态 ,是否已成功完成?
因为在事务状态的基础上,如果事务完成,那么我需要执行重定向到另一个页面,否则如果事务未成功完成,那么我需要在页面上显示错误。
我想在以下内容后重定向: -
的 scope.Complete(); 的的
的 scope.Dispose(); 的的
请在这方面帮助我。
答案 0 :(得分:10)
如果TransactionScope
为try
{
// Create the TransactionScope to execute the commands, guaranteeing
// that both commands can commit or roll back as a single unit of work.
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection connection1 = new SqlConnection(connectString1))
{
// Opening the connection automatically enlists it in the
// TransactionScope as a lightweight transaction.
connection1.Open();
// Create the SqlCommand object and execute the first command.
SqlCommand command1 = new SqlCommand(commandText1, connection1);
returnValue = command1.ExecuteNonQuery();
writer.WriteLine("Rows to be affected by command1: {0}", returnValue);
// If you get here, this means that command1 succeeded. By nesting
// the using block for connection2 inside that of connection1, you
// conserve server and network resources as connection2 is opened
// only when there is a chance that the transaction can commit.
using (SqlConnection connection2 = new SqlConnection(connectString2))
{
// The transaction is escalated to a full distributed
// transaction when connection2 is opened.
connection2.Open();
// Execute the second command in the second database.
returnValue = 0;
SqlCommand command2 = new SqlCommand(commandText2, connection2);
returnValue = command2.ExecuteNonQuery();
writer.WriteLine("Rows to be affected by command2: {0}", returnValue);
}
}
// The Complete method commits the transaction. If an exception has been thrown,
// Complete is not called and the transaction is rolled back.
scope.Complete();
}
}
catch (TransactionAbortedException ex)
{
writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
}
catch (ApplicationException ex)
{
writer.WriteLine("ApplicationException Message: {0}", ex.Message);
}
,您会发现这个记录良好的示例:
scope.Complete()
包含最多值的评论是这一个:
Complete方法提交事务。如果抛出异常,则不调用Complete,并回滚事务。
因此,如果没有抛出异常,您可以继续。在Complete()
之后重定向。如果抛出异常,则事务失败并自动回滚。您可以在致电if (Transaction.Current.TransactionInformation.Status == TransactionStatus.Committed)
{
// do redirect
}
之后以及通过visit the MSDN page重定向之前仔细检查交易状态(正如其他人发布的那样):
{{1}}
答案 1 :(得分:3)
使用Transaction.Current.TransactionInformation.Status
答案 2 :(得分:2)
怎么样:
TransactionStatus status = Transaction.Current.TransactionInformation.Status;
答案 3 :(得分:2)
我发现最有效/正确捕获此方法的最佳方法如下:
在transactionscope内部使用语句,并在调用scope / Complete()之前。
//Register for the transaction completed event for the current transaction
Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Current_TransactionCompleted);
然后按如下方式创建事件处理函数:
/// <summary>
/// Handles the TransactionCompleted event of the Current control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Transactions.TransactionEventArgs"/> instance containing the event data.</param>
static void Current_TransactionCompleted(object sender, TransactionEventArgs e)
{
if (e.Transaction.TransactionInformation.Status == TransactionStatus.Committed)
{
/// Yay it's committed code goes here!
}
}
引用MSDN
“您可以注册此事件,而不是使用volatile登记来获取事务的结果信息。传递给TransactionCompletedEventHandler委托的参数是一个Transaction实例。然后,您可以查询特定实例的TransactionInformation属性以获取实例TransactionInformation,其Status属性包含具有Committed或Aborted值的事务的状态。“