Friday, October 12, 2012

transactions through SQLConnection with the ability to rollback upon failure

We (Santanna Energy) are doing some lightweight SOA (service-oriented architecture) stuff in the name of making sure that we rollback writes via stored procedures if a subsequent push to Microsoft Dynamics GP (Great Plains) fails. Nathan Carroll has implemented something of this approach:

using (var Conn = new SqlConnection(_ConnectionString))
{
   SqlTransaction trans = null;
   try
   {
      Conn.Open();
      trans = Conn.BeginTransaction();
      
      using (SqlCommand Com = new SqlCommand(ComText, Conn, trans))
      {
         
/* DB work */
      }
      trans.Commit();
   }
   catch (Exception Ex)
   {
      if (trans != null) trans.Rollback();
      return -1;
   }
}

No comments:

Post a Comment