Please see my other C# articles.
When a feature requires an atomic action, ADO.NET transactions easily ensure data concurrency while performing writes against the database.
For example, let’s say two separate db writes are needed, each of which executes a stored procedure.
Should errors occur, it’s essential that both writes are rolled-back before any changes are committed to the db. Conversely, if both actions are free from error, then both changes should be committed simultaneously.
In the example below, you will see code that illustrates the previous principles.
First, the’re the function implementing transactions and calling two separate functions.
Below that are the two functions executing sprocs, each of which is contained within a transaction so that if it should fail, it and the other changes would automatically get rolled back, preserving data concurrency.
The main function which implements transactions. Within that block are the two functions that are called leveraging Generics below.