A transaction is a set of operations that must either
succeed or fail as a unit. The goal of a transaction is to ensure that data is
always in a valid, consistent state. For example, consider a transaction that
transfers $1000 from account A to account B; clearly there are two transactions.
1) It should deduct $1000 from account A and 2) it should add $1000 to account
B. Suppose that a transaction completes step 1, but fails in step 2 because of
some errors. This leads to inconsistent data because the total amount of money
is no longer accurate. A full $1000 has gone missing. Transactions help avoid
these types of problems by ensuring that changes are committed to a database
only if all the steps are successful. So in this example, if step 2 fails, then
the changes made by step 1 will not be committed to the database. This ensures
that the system stays in any of two valid states --- the initial state (with no
money transferred) and the final state (with money debited from the account and
credited to another). The sample code snippets have been written in C#.