我如何跨多种方法使用Sql Transaction

时间:2015-01-09 10:04:54

标签: c# using sqlcommand sqltransaction

假设我们有一个对象A可以删除一个对象B,该对象B保存一个来自A的forendkey

如果你想删除A,你必须首先从B中删除forendkey然后你可以删除A但是如果出现问题它应该回滚但我也想使用来自B的删除forendkey但是在那一刻我不知道如何实现这个目标

我目前的想法:

    public void DeleteA(Object a)
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.open();

            using (SqlTransaction tr = con.BeginTransaction())
            {
                try
                {
                    DeleteAfromAllB(a, con, tr);

                    using (SqlCommand cmd = new SqlCommand("STP_A_Delete", con))
                    {
                        cmd.Transaction = tr;

                        // some parameters
                        // some sort of Execute
                        // e.g.: cmd.ExecuteNonQuery();
                    }
                    tr.Commit();
                }
                catch (SqlException ex)
                {
                    //ExceptionHandling
                }
            }
        }
    }

    private void DeleteAfromAllB(Object a, SqlConnection con, SqlTransaction tr)
    {
        try
        {
            using (SqlCommand cmd = new SqlCommand("STP_B_Delete_Referenc_To_A", con))
            {
                cmd.Transaction = tr;

                // some parameters
                // some sort of Execute
                // e.g.: cmd.ExecuteNonQuery();
            }
        }
        catch (SqlException ex)
        {
            //ExceptionHandling
        }
    }       

    public void DeleteAfromAllB(Object a)
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.open();

            using (SqlTransaction tr = con.BeginTransaction())
            {
                DeleteAfromAllB(a,con,tr);

                tr.Commit();
            }
        }
    }

但是你可以看到这很难看

1 个答案:

答案 0 :(得分:1)

电话

public void DeleteAfromAllB(Object a) 

不需要传递SqlConnection,因为您可以从tr.Connection引用。所以你只需要SqlTransaction作为参数。所以对于你原来的问题,是的,我认为传递SqlTransaction是要走的路。我个人更喜欢这种方式,因为您可以轻松跟踪事务的调用堆栈/范围(即事务开始/完成的位置)。

另一种方法是使用TransactionScope。

E.g。

private void DeleteAfromAllB(Object a)
{
    try
    {
        using (var con = new SqlConnection())
        {
            con.open();
            using (var cmd = new SqlCommand("STP_B_Delete_Referenc_To_A", con))
            {
                // some parameters
                // some sort of Execute
                // e.g.: cmd.ExecuteNonQuery();
            }
        }
        catch (SqlException ex)
        {
            //ExceptionHandling
        }
    }
}

public void DeleteAfromAllB_TopLevel(Object a)
{
    using (var scope = new TransactionScope())
    {
        DeleteAfromAllB(a);

        // The Complete method commits the transaction. If an exception has been thrown, 
        // Complete is not  called and the transaction is rolled back.
        scope.Complete();
    }
}