none
SqlTransaction wrapped in try-catch? RRS feed

  • Question

  • Hi,

    I'm reading various examples of using the SqlTransaction and they all pretty much uses code like this:

    using (SqlConnection cn = new SqlConnection(constr)) {
        cn.Open();
        using (SqlTransaction tr = cn.BeginTransaction()) {
            //some code
            tr.Commit();
        }
    }

    But on MSDN here, I read that I should try-catch and manually roll-back (look for the Note). Which is correct?

    --
    Werner

    Tuesday, December 4, 2012 9:30 AM

Answers

  • Use the method in the documentation and explicitly perform the rollback. The above method relies on the transaction being disposed before a commit (for the rollback). It's default behavior and default behavior can change.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Werner Clausen Tuesday, December 4, 2012 2:28 PM
    Tuesday, December 4, 2012 1:42 PM