Library code snippets
Transactions made easy with .NET 2.0
One of the more significant improvement in .NET 2.0 is the transactions area. Now with a single line it becomes extremely easy to support transactional code blocks using the concept of “ambient” transaction thanks to TransactionScope in the System.Transactions namespace.
Check out the following code:
using (TransactionScope ts = new TransactionScope()) {
// An "ambient" transaction is placed in the current call context
DbProviderFactory provider;
provider = DbProviderFactories.GetFactory("System.Data.SqlClient");
DbConnection conn = provider.CreateConnection();
conn.ConnectionString = strConn;
// First query. This one will succeed.
DbCommand dbcmd = conn.CreateCommand();
dbcmd.Connection = conn;
dbcmd.CommandText = "DELETE Products";
dbcmd.CommandType = CommandType.Text;
// Second query. This one will fail.
DbCommand dbcmd2 = conn.CreateCommand();
dbcmd2.Connection = conn;
dbcmd2.CommandText = "DELETE INVALIDTABLE";
dbcmd2.CommandType = CommandType.Text;
conn.Open();
try {
// Let's empty the Products table using the query #1.
dbcmd.ExecuteNonQuery();
// The second query will try to empty a non-existent table.
// It will fail and the ts.Complete() method wont be executed.
dbcmd2.ExecuteNonQuery();
//If all the operations succeded,
//then let's commit the transaction.
ts.Complete();
} catch (DbException ex) {
// Error handling block
} finally {
// Cleanup
conn.Close();
ts.Dispose();
}
}
Extremely simple, isn't it? TransactionScope will take care of almost all the transactional stuff in this code block. All that is required to commit the transaction is to call the ts.Complete() method. Notice that the connection object itself is confined within the scope so it automatically participates in the transaction.
You can manipulate the transaction context with Transaction.Current. Please be aware that this is not limited to SQL Server operations. You can create transaction for Oracle, SQL Server data-storages, MSMQ messaging and even bulk copying filesystem operations.
I hope you found this article useful. Happy coding!
Related articles
Related discussion
-
hey developers out there
by pitsophera (0 replies)
-
Using ADO.NET with SQL Server
by Manjot Bawa (23 replies)
-
High-Performance .NET Application Development & Architecture
by Manjot Bawa (0 replies)
-
An Introduction to VB.NET and Database Programming
by carlosmen (14 replies)
-
Chart insertation in a windows form...
by pdhanik (1 replies)
Related podcasts
-
ADO.NET Data Services in .NET 3.5 Service Pack 1 Beta1 with ASP.NET AJAX
Wally walks through using ASP.NET Podcast Show #114 - ADO.NET Data Services in .NET 3.5 Service Pack 1 Beta1 with ASP.NET AJAX.
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
This thread is for discussions of Transactions made easy with .NET 2.0 .