Home > Articles > Programming > Windows Programming

ADO.NET Transactions

  • Print
  • + Share This
When your code is doing ad hoc SQL, and you aren't using stored procedures, using ADO.NET SqlTransaction objects is the easiest way to roll the execution of multiple SQL statements into one transaction.
Rick Leinecker is currently writing ASP.NET Solutions – 24 Case Studies (Addison-Wesley, scheduled for publication May 2003, ISBN 0-321-15965-9).
Like this article? We recommend

Like this article? We recommend

Here's the Scenario

I'm in front of 25 students in an ASP.NET class. They all work for Fortune 1,000 companies, and are seasoned programmers. We're building an application that accesses a database with five tables, four of which are keyed to the User table.

We're working through the ADO.NET code that creates a record in the User table and then creates the four records in the other tables. We finish this part of the code, and as I wait for questions, one of the students asks what happens if a row can't be inserted into any of the four child tables.

In a crowded classroom, you respond quickly to questions. I tell him not to worry—that couldn't possibly happen. He doesn't challenge me, and we finish the application.

Now it's time for testing. We create a number of records with the application, but somewhere along the way an exception is thrown and only two of the four child tables have records inserted. And unfortunately, based on the way the database has been set up, everything is totally hosed and we can't add any more records.

If you've ever been in this situation, you know what I felt like. If you haven't been in this situation, imagine wanting to slide under the rug, or just disappear.

  • + Share This
  • 🔖 Save To Your Account