Tuesday, September 15, 2009

Playing with Transaction in .Net 2.0

That's why I love so much about .Net. Recently I came across a scenario where one of my junior colleague is using the old and little bit lengthy way of implementing transaction in C# code. I suggested her to use the single liner crispy TransactionScope in the System.Transactions namespace. I'm just adding a sample code snippet here


Pretty easy, don't you think so? TransactionScope will take care of all the transactional related stuff in the above code block. We need to commit the transaction by calling the ts.Complete() method. As the connection object itself is confined within the scope so it automatically participates in the transaction.(Kindly note as its a code snippet so everything is not defined here properly as its only part of the whole demo project)
Please explore more the transaction context with Transaction.Current. And last but not the least thing that this feature is not limited to SQL Server operations. You can use the same transaction for Oracle, SQL Server data-storages, MSMQ messaging and even bulk copying filesystem operations.


No comments:

FEEDJIT Live Traffic Feed