ADO.NET Transactions
- Here's the Scenario
- The Solution
- Using the SqlTransaction Object
- Summary
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 worrythat 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.