How to rollback the transaction using?

Asked

Viewed 40 times

4

How can I give RollBack() in a transaction initiated in the declaration using?

I know I can keep the transaction variable out of the using of SqlConnection, but it is possible RollBack() using the using?

My code

        using (SqlConnection cnn = Geral.conexaoSql())
        using (SqlTransaction tran = cnn.BeginTransaction())
        {
            _clienteBLL.ApagarCliente(cliente, cnn, tran);

            tran.Commit();
        }

1 answer

1

You can put a block try catch and within the catch call the method RollBack of SQLTransaction:

using (SqlConnection cnn = Geral.conexaoSql())
using (SqlTransaction tran = cnn.BeginTransaction())
{
    try
    {
        _clienteBLL.ApagarCliente(cliente, cnn, tran);

        tran.Commit();
    }
    catch
    {
        tran.RollBack();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.