不能访问的SqlTransaction对象catch块回滚 [英] Cannot access SqlTransaction object to rollback in catch block

查看:130
本文介绍了不能访问的SqlTransaction对象catch块回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我发现所有文章或例子似乎并不关心它。

我想做的事情在一个事务中的一些数据库操作。我想要做的是非常相似,大多数的例子:

 使用(SqlConnection的康恩=新的SqlConnection(_ConnectionString))
{
    尝试
    {
        Conn.Open();
        的SqlTransaction反式= Conn.BeginTransaction();

        使用(的SqlCommand的COM =新的SqlCommand(ComText,康涅狄格州))
        {
            / * DB工作* /
        }
    }
    赶上(例外前)
    {
        Trans.Rollback();
        返回-1;
    }
}
 

但问题是,的SqlTransaction跨尝试块内声明。因此,它是不可访问,在赶上()块。大多数的例子只是做 Conn.Open() Conn.BeginTransaction()尝试块,但我认为这是一个有点冒险,因为两者皆可抛多个异常。

我错了,或者说大多数人只是忽视这种风险? 什么是最好的解决方案要能够回滚,如果有异常情况发生?

解决方案

 使用(VAR康恩=新的SqlConnection(_ConnectionString))
{
    的SqlTransaction反= NULL;
    尝试
    {
        Conn.Open();
        反式= Conn.BeginTransaction();

        使用(的SqlCommand的COM =新的SqlCommand(ComText,康涅狄格州,反式))
        {
            / * DB工作* /
        }
        trans.Commit();
    }
    赶上(例外前)
    {
        如果(!反式= NULL)trans.Rollback();
        返回-1;
    }
}
 

或者你可以去更清洁和更方便,使用这样的:

 使用(VAR康恩=新的SqlConnection(_ConnectionString))
{
    尝试
    {
        Conn.Open();
        使用(VAR TS =新System.Transactions.TransactionScope())
        {
            使用(的SqlCommand的COM =新的SqlCommand(ComText,康涅狄格州))
            {
                / * DB工作* /
            }
            ts.Complete();
        }
    }
    赶上(例外前)
    {
        返回-1;
    }
}
 

I've got a problem, and all articles or examples I found seem to not care about it.

I want to do some database actions in a transaction. What I want to do is very similar to most examples:

using (SqlConnection Conn = new SqlConnection(_ConnectionString))
{
    try
    {
        Conn.Open();
        SqlTransaction Trans = Conn.BeginTransaction();

        using (SqlCommand Com = new SqlCommand(ComText, Conn))
        {
            /* DB work */
        }
    }
    catch (Exception Ex)
    {
        Trans.Rollback();
        return -1;
    }
}

But the problem is that the SqlTransaction Trans is declared inside the try block. So it is not accessable in the catch() block. Most examples just do Conn.Open() and Conn.BeginTransaction() before the try block, but I think that's a bit risky, since both can throw multiple exceptions.

Am I wrong, or do most people just ignore this risk? What's the best solution to be able to rollback, if an exception happens?

解决方案

using (var Conn = new SqlConnection(_ConnectionString))
{
    SqlTransaction trans = null;
    try
    {
        Conn.Open();
        trans = Conn.BeginTransaction();

        using (SqlCommand Com = new SqlCommand(ComText, Conn, trans))
        {
            /* DB work */
        }
        trans.Commit();
    }
    catch (Exception Ex)
    {
        if (trans != null) trans.Rollback();
        return -1;
    }
}

or you could go even cleaner and easier and use this:

using (var Conn = new SqlConnection(_ConnectionString))
{
    try
    {
        Conn.Open();
        using (var ts = new System.Transactions.TransactionScope())
        {
            using (SqlCommand Com = new SqlCommand(ComText, Conn))
            {
                /* DB work */
            }
            ts.Complete();
        }
    }
    catch (Exception Ex)
    {     
        return -1;
    }
}

这篇关于不能访问的SqlTransaction对象catch块回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆