31.2 Properties Reference

Connection

SqlConnection sqlConnection = Transaction.Connection;

Gets the connection object associated with the transaction.

Example

The following example demonstrates how to retrieve the connection object for a transaction:

String connString = "Data Source=(local);Integrated security=SSPI;" + 
    "Initial Catalog=Northwind;";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlTransaction tran = conn.BeginTransaction();

// returns a reference to the SqlConnection conn
SqlConnection conn2 = tran.Connection;

Note

This value is null if the connection is no longer valid.

IsolationLevel

IsolationLevel isolationLevel = Transaction.IsolationLevel;

Gets the isolation level for the transaction. This value is one of the IsolationLevel enumeration values described in Table 31-4.

Table 31-4. IsolationLevelEnumeration

Name

Description

ReadUncommitted

No shared locks are issued, and exclusive locks aren't honored. A dirty read is possible.

ReadCommitted

Shared locks are held while data is being read by the transaction. Dirty reads aren't possible, but non-repeatable reads or phantom rows can occur because data can be changed before it is committed.

RepeatableRead

Shared locks are placed on all data used in a query, preventing other users from updating the data. Nonrepeatable reads are prevented, but phantom reads are still possible.

Serializable

A range lock, in which the individual records and the ranges between records are covered, is placed on the data preventing other users from updating or inserting rows until the transaction is complete. Phantom reads are prevented.

Chaos

Pending changes from more highly isolated transactions can't be overwritten. Not supported by SQL Server.

Unspecified

A different isolation level than the one specified is used, but that level can't be determined.

Example

The following example demonstrates how to set the IsolationLevel for a new transaction:

String connString = "Data Source=(local);Integrated security=SSPI;" + 
    "Initial Catalog=Northwind;";

SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlTransaction tran =
    conn.BeginTransaction(IsolationLevel.RepeatableRead);

// returns IsolationLevel.RepeatableRead
IsolationLevel il = tran.IsolationLevel;

Note

The default value of the IsolationLevel property is ReadCommitted.



    Part I: ADO.NET Tutorial
    Part II: ADO.NET Core Classes
    Part III: API Quick Reference