1

Let's assume the following code :

try 
{
    using(SqlConnection conn = new SqlConnection(...))
    using(SqlTransaction trans = conn.BeginTransaction())
    using(SQlCommand cmd = new SqlCommand())
    {
        ...
        Sql Exception occurs !
        trans.Commit()
    }
} catch (SqlException) 
{
}

Will there be an automatic rollback because of this exception or is it mandatory to call trans.Rollback() ?

SeyoS
  • 661
  • 5
  • 22
  • Copy-paste the title of your question into Google and you'll get a myriad of duplicates, both in StackOverflow and outside. Searching is a critical skill if you expect to become a good programmer – Camilo Terevinto Dec 10 '21 at 16:34

1 Answers1

2

From the documentation:

The transaction is rolled back on any error, or if it is disposed without first being committed.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62