No getconnection any more and transaction do not have save point

Topics: Data Access Application Block
Aug 1, 2012 at 7:53 AM

http://msdn.microsoft.com/en-us/magazine/cc188702.aspx

i follow above article, discover no db.GetConnection(); any more

and transaction do not have save point

as like

     

 Trans.Save("0");
                    command.ExecuteNonQuery();

      try
      {
                     trans.Commit();
      }catch (Exception ex)
      {
                            Trans.Rollback("0");
      }
Aug 1, 2012 at 12:37 PM

The referenced article is from 2005 using a much older version of Enterprise Library (version 2, I think).  I recommend reading Much ADO about Data Access in the Enterprise Library 5.0 Developers Guide.

You can see an example there for using transactions:

using (DbConnection conn = db.CreateConnection())
{
  conn.Open();
  DbTransaction trans = conn.BeginTransaction();

  try
  {
    // execute commands, passing in the current transaction to each one
    db.ExecuteNonQuery(cmdA, trans);
    db.ExecuteNonQuery(cmdB, trans);
    trans.Commit();    // commit the transaction
  }
  catch
  {
    trans.Rollback();  // rollback the transaction
  }
}

Or a TransactionScope can also be used to achieve a similar result:

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
  db.ExecuteNonQuery(cmdA);
  db.ExecuteNonQuery(cmdB);
  scope.Complete();
}

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com