3.11. Using Transactions

The IDbConnection interface has a single method to begin a local transaction. This method returns an instance of a class that implements IDbTransaction. Classes such as SqlTransaction and OleDbTransaction implement IDbTransaction, which encapsulates operations involving a local transaction such as Commit and Rollback. You can configure the transaction isolation level through an optional parameter of IDbConnection.BeginTransaction. A simple transaction is shown in Listing 3-26.

Listing 3-26. A simple transaction
 SqlConnection sconn = new SqlConnection( "server=(local);uid=sa;pwd=mypw;database=pubs"); SqlCommand cmd = new SqlCommand( "insert jobs values('First', 10, 10)", sconn); SqlTransaction tx = null; try { sconn.Open(); ...

Get Essential ADO.NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.