Cannot access SqlTransaction object to rollback in catch block

前端 未结 7 1053
我寻月下人不归
我寻月下人不归 2020-12-02 15:43

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 s

相关标签:
7条回答
  • 2020-12-02 16:26
    SqlConnection conn = null;
    SqlTransaction trans = null;
    
    try
    {
       conn = new SqlConnection(_ConnectionString);
       conn.Open();
       trans = conn.BeginTransaction();
       /*
        * DB WORK
        */
       trans.Commit();
    }
    catch (Exception ex)
    {
       if (trans != null)
       {
          trans.Rollback();
       }
       return -1;
    }
    finally
    {
       if (conn != null)
       {
          conn.Close();
       }
    }
    
    0 讨论(0)
提交回复
热议问题