18

I am developing an application which communicates with an SQL Server 2005 database to execute some stored procedures. My client demands that all transactions be managed on the C# side and not by SQL Server, and so I am using System.Transactions.TransactionScope when accessing the database. However, I have just seen the System.Transactions.Transaction datatype, and I am confused... What are the main pros/cons of each type? Which one should I use?

Please note that I must also use Enterprise Library's Data Access Application Block.

User
  • 3,244
  • 8
  • 27
  • 48
  • 1
    See also http://stackoverflow.com/questions/224689/transactions-in-net – Marc Gravell Jun 27 '11 at 13:23
  • Looks like a repeat of http://stackoverflow.com/questions/542525/transactionscope-vs-transaction-in-linq-to-sql – Dave Jun 27 '11 at 13:04
  • 1
    `My client demands that ...` .. why? What is it that the client *really* wants / is afraid of? It's almost certain that they don't *actually* want that directly - they want something else and have decided that forcing the Transaction into C# is the way to do it. "E.g. I read somewhere that SQL Transactions aren't reliable"... etc. I doubt that you'll be able to resolve their underlying fears / desires unless you understand what they really want. – Brondahl Jul 08 '20 at 10:08
  • a simple answer to your question could be "we have a programmer that only understands one of them on staff". Or more relevantly, they want all of the code modifying the database in the same place so it's easier to work on. These "why why why" comments are incredibly unnecessary and irrelevant. – John Lord May 04 '23 at 14:42

1 Answers1

20

From msdn :

The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself. A transaction scope can select and manage the ambient transaction automatically. Due to its ease of use and efficiency, it is recommended that you use the TransactionScope class when developing a transaction application. When you instantiate TransactionScope, the transaction manager determines which transaction to participate in. Once determined, the scope always participates in that transaction. The decision is based on two factors: whether an ambient transaction is present and the value of the TransactionScopeOption parameter in the constructor. The ambient transaction is the transaction within which your code executes. You can obtain a reference to the ambient transaction by calling the static Current property of the Transaction class.

You can read more about that here :

http://msdn.microsoft.com/en-us/library/ms172152(v=vs.90).aspx

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope(v=vs.90).aspx

Great (a bit old) article about transaction in .NET 2.0

http://msdn.microsoft.com/en-us/library/ms973865.aspx

Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73