CHAPTER 21 ADO.NET PART I: THE CONNECTED LAYER
It turns out that the .NET platform supports transactions in a variety of ways. This chapter will look
at the transaction object of your ADO.NET data provider (SqlTransaction, in the case of
System.Data.SqlClient). The .NET base class libraries also provide transact ional support within
numerous APIs, including the following:
•
System.EnterpriseServices: This namespace (located in the
System.EnterpriseServices.dll assembly) provides types that allow you to
integrate with the COM+ runtime layer, including its support for distributed
transactions.
•
System.Transactions: This namespace (located in the System.Transactions.dll
assembly) contains classes that allow you to write your own transactional
applications and resource managers for a variety of services (e.g., MSMQ,
ADO.NET, and COM+).
•
Windows Communication Foundation: The WCF API provides services to facilitate
transactions with various distributed binding classes.
•
Windows Workflow Foundations: The WF API provides transactional support for
workflow activities.
In addition to the baked-in transactional support found within the .NET base class libraries, it is also
possible to use the SQL language of your database management system. For example, you could author
a stored procedure that uses the BEGIN TRANSACTION, ROLLBACK, and COMMIT statements.
Key Members of an ADO.NET Transaction Object
While transactional-aware types exist throughout the base class libraries, you will focus on transaction
objects found within an ADO.NET data provider, all of which derive from DBTransaction and implement
the IDbTransaction interface. Recall from the beginning of this chapter that IDbTransaction defines a
handful of members as follows:
public interface IDbTransaction : IDisposable
{
IDbConnection Connection { get; }
IsolationLevel IsolationLevel { get; }
void Commit();
void Rollback();
}
Notice the Connection property, which returns a reference to the connection object that initiated the
current transaction (as you’ll see, you obtain a transaction object from a given connection object). You
call the Commit() method when each of your database operations have succeeded. Doing this causes
each of the pending changes to be persisted in the data store. Conversely, you can call the Rollback()
method in the event of a runtime exception, which informs the DMBS to disregard any pending changes,
leaving the original data intact.
854