SQLTransaction,Transactionscope & IDBtransaction
What is the difference between SQLTransaction,Transactionscope & IDBtransaction in c#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vinay SinghRank #604 · 4K reputationPosted Jun 14, 2016, 10:21 PM EDT
http://stackoverflow.com/questions/1270981/refactoring-ado-net-sqltransaction-vs-transactionscope
http://stackoverflow.com/questions/542525/transactionscope-vs-transaction-in-linq-to-sql
http://www.eranachum.com/PermaLink,guid,6e26a414-6a87-47a6-b9c0-e3be26298852.aspx
Vasanth NatarajanRank #3,352 · 429 reputationPosted Jun 14, 2016, 11:00 PM EDT
Transaction
If anything goes wrong with any of the grouped statements, all changes need to be aborted. The process of reversing changes is called rollback in SQL Server terminology. If everything is in order with all statements within a single transaction, all changes are recorded together in the database. In SQL Server terminology, we say that these changes are committed to the database.
using (TransactionScope transactionScope = new TransactionScope())
{
try
{
orcmethod1() // Oracle DML operation
sqlmethod2() // Sql DML operation
orcmethod3() // Oracle DML operation
// transactionScope.complete()
// transactionScope.Dispose();
}
catch (TransactionException ex)
{
transactionScope.Dispose();
MessageBox.Show("Transaction Exception Occured");
}
}I'm entirely with you on the connection; that should be
using, and there is no need for the explicitClose(). The transaction is a little bit trickier; the code shown is certainly overkill at the moment, but it is not entirely defined thatDispose()should do a rollback. Actually, that is what tends to happen in every implementation I've looked at, but it is slightly vexing that evenDbTransaction(which most providers use) doesn't actually do this. Contrast toTransactionScopewhere it is explicitly defined that aDispose()without a commit counts as a rollback. Because of that, I tend to use (excuse the C#):using(var conn = GetOpenConnection()) using(var tran = conn.BeginTransaction()) { try { // TODO: do work tran.Commit(); } catch { tran.Rollback(); throw; } }rupali patil174 reputationPosted Jun 15, 2016, 11:42 PM EDT