Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

The Solution

What I needed was a way to create an "all or nothing" situation. All five table inserts needed to be part of a transaction. If any one of the inserts failed, the others would all be rolled back or aborted. I needed a transaction that included all five inserts.

SQL Server transactions can be accomplished with SQL, and are usually most effective when they're part of a stored procedure. But there are times when your code is doing ad hoc SQL, and you aren't using stored procedures. When this is the case, adding the SQL to support transactions is complicated. In many cases, using ADO.NET SqlTransaction objects is the easiest way to roll the execution of multiple SQL statements into one transaction.

After a lot of beating around the bush in class, I added the SqlTransaction objects, cleared the database tables, and started all over. Besides gracefully handling the problem, I was showing my students how easy it is to use transactions with ADO.NET.

  • + Share This
  • 🔖 Save To Your Account