Reputation: 1519
What is the difference among the following BeginTransaction methods:
SqlConnection.BeginTransaction Method
DbConnection.BeginTransaction Method
DbConnection.BeginDbTransaction Method
Moreover, how they are different from TransactionScope() method in System.Transaction?
Upvotes: 2
Views: 3660
Reputation: 5567
SqlConnection.BeginTransaction
creates a SqlTransaction
, which is specific to MS SQL ServerDbConnection.BeginTransaction
creates a DbTransaction
, which is generic, and relies on the underlying connection to create a database-specific transaction. If your DbConnection
is of type SqlConnection
, this will be a SqlTransaction
.DbConnection.BeginDbTransaction
is a protected method that you override if you're creating your own class that inherits from DbConnection
.EDIT:
These are all specific to the database connection from which they were created, which is used differently than a TransactionScope, which isn't database-dependent. I believe if you wanted to coordinate transactions between multiple connections, you have to explicitly call DbConnection.EnlistTransaction(transaction)
. With a TransactionScope, a connection will (depending on the database provider, at least it should) automatically enlist in the TransactionScope if one exists when the connection is opened. In WCF, a TransactionScope can also be passed across service boundaries, and can be used to commit the results of multiple service calls as a single transaction.
Upvotes: 10