What is the difference among the following BeginTransaction methods:
SqlConnection.BeginTransaction Method
DbConnection.BeginTransaction 开发者_JS百科Method
DbConnection.BeginDbTransaction Method
Moreover, how they are different from TransactionScope() method in System.Transaction?
SqlConnection.BeginTransaction
creates aSqlTransaction
, which is specific to MS SQL ServerDbConnection.BeginTransaction
creates aDbTransaction
, which is generic, and relies on the underlying connection to create a database-specific transaction. If yourDbConnection
is of typeSqlConnection
, this will be aSqlTransaction
.DbConnection.BeginDbTransaction
is a protected method that you override if you're creating your own class that inherits fromDbConnection
.
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.
精彩评论