title: Cannot access SqlTransaction object to rollback in catch block
body: <p>I've got a problem, and all articles or examples I found seem to not care about it.</p>
  
  <p>I want to do some database actions in a transaction. What I want to do is very similar to most examples:</p>
  
  <pre><code>using (SqlConnection Conn = new SqlConnection(_ConnectionString))
  {
      try
      {
          Conn.Open();
          SqlTransaction Trans = Conn.BeginTransaction();
  
          using (SqlCommand Com = new SqlCommand(ComText, Conn))
          {
              /* DB work */
          }
      }
      catch (Exception Ex)
      {
          Trans.Rollback();
          return -1;
      }
  }
  </code></pre>
  
  <p>But the problem is that the <code>SqlTransaction Trans</code> is declared inside the <code>try</code> block. So it is not accessable in the <code>catch()</code> block. Most examples just do <code>Conn.Open()</code> and <code>Conn.BeginTransaction()</code> before the <code>try</code> block, but I think that's a bit risky, since both can throw multiple exceptions.</p>
  
  <p>Am I wrong, or do most people just ignore this risk?
  What's the best solution to be able to rollback, if an exception happens?</p>