.net 确定是否提交了TransactionScope

.net 确定是否提交了TransactionScope,.net,sql-server,ado.net,transactionscope,.net,Sql Server,Ado.net,Transactionscope,使用.NET2和ADO.net 有没有办法确定事务是否已提交?原因是我被一个我无法更改的遗留框架所困扰,可能存在也可能不存在环境事务。有时,环境事务已经提交,导致下一个数据库调用抛出异常,我需要知道是否存在异常 任何指针都会很棒 谢谢 Johan检查事务.当前.事务信息.状态。如果它不是事务状态.Active,则不应使用当前事务 不用说,在获取其状态之前,您应该检查Transaction.Current中的null。检查Transaction.Current.TransactionInforma

使用.NET2和ADO.net

有没有办法确定事务是否已提交?原因是我被一个我无法更改的遗留框架所困扰,可能存在也可能不存在环境事务。有时,环境事务已经提交,导致下一个数据库调用抛出异常,我需要知道是否存在异常

任何指针都会很棒

谢谢


Johan

检查
事务.当前.事务信息.状态
。如果它不是
事务状态.Active
,则不应使用当前事务


不用说,在获取其状态之前,您应该检查
Transaction.Current
中的
null

检查
Transaction.Current.TransactionInformation.status
。如果它不是
事务状态.Active
,则不应使用当前事务


不言而喻,在获取其状态之前,您应该检查
Transaction.Current
是否为
null

我找到的最有效/正确捕获此状态的最佳方法如下:

/// <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!
    }
}
在transactionscope using语句内部,调用scope/Complete()之前

然后创建事件处理程序函数,如下所示:

/// <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!
    }
}
//
///处理当前控件的TransactionCompleted事件。
/// 
///事件的来源。
///包含事件数据的实例。
静态无效当前交易已完成(对象发送方,交易目标e)
{
if(e.Transaction.TransactionInformation.Status==TransactionStatus.Committed)
{
///耶,这是承诺代码!
}
}
引用


“您可以注册此事件,而不是使用易失性登记来获取事务的结果信息。传递给TransactionCompletedEventHandler委托的参数是事务实例。然后,您可以查询特定实例的TransactionInformation属性,以获取TransactionInformation实例,其Status属性包含具有已提交或已中止值的事务的状态。”

我找到的最有效/正确捕获该值的最佳方法如下:

/// <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!
    }
}
在transactionscope using语句内部,调用scope/Complete()之前

然后创建事件处理程序函数,如下所示:

/// <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!
    }
}
//
///处理当前控件的TransactionCompleted事件。
/// 
///事件的来源。
///包含事件数据的实例。
静态无效当前交易已完成(对象发送方,交易目标e)
{
if(e.Transaction.TransactionInformation.Status==TransactionStatus.Committed)
{
///耶,这是承诺代码!
}
}
引用


“您可以注册此事件,而不是使用易失性登记来获取事务的结果信息。传递给TransactionCompletedEventHandler委托的参数是事务实例。然后,您可以查询特定实例的TransactionInformation属性以获取TransactionInformation实例,其Status属性包含具有已提交或已中止值的事务的状态。“

发布一些当前代码库的示例会有所帮助。发布一些当前代码库的示例会有所帮助。