Welcome to MSDN Blogs Sign in | Join | Help

A Simpler TransactionScope

In .Net 3.5 I can write a transactional code block as follows:

transacted(()=>
{
   using (SqlConnection connection = new SqlConnection(connectionString))
   {
      connection.Open();

      SqlCommand command1 = new SqlCommand(commandString1, connection);
      command1.ExecuteNonQuery();

      SqlCommand command2 = new SqlCommand(commandString2, connection);
      command2.ExecuteNonQuery();
   }
});

No need for using and no need to remember to call Complete at the end of the block.

To enable this I need to write just a few lines of code:

delegate void TransactedCodeDelegate();
void transacted(TransactedCodeDelegate txCode)
{
   using (TransactionScope ts = new TransactionScope())
   {
      txCode();
      ts.Complete();
   }
}
Published Monday, March 24, 2008 12:33 PM by florinlazar
Filed under:

Comments

# MSDN Blog Postings » A Simpler TransactionScope

Monday, March 24, 2008 5:54 PM by MSDN Blog Postings » A Simpler TransactionScope

# re: A Simpler TransactionScope

Friday, May 02, 2008 7:27 PM by florinlazar

And yes, you can do this in C# 2.0 too using "delegate()" instead of "()=>".

# Limitation of TransactionScope (and using)

Monday, May 05, 2008 4:39 AM by Florin Lazar

If you read the documentation for TransactionScope , you will find: "If no exception occurs within the

Anonymous comments are disabled
 
Page view tracker