Export (0) Print
Expand All

OdbcConnection.BeginTransaction Method (IsolationLevel)

Starts a transaction at the data source with the specified IsolationLevel value.

Namespace: System.Data.Odbc
Assembly: System.Data (in system.data.dll)

public OdbcTransaction BeginTransaction (
	IsolationLevel isolevel
)
public OdbcTransaction BeginTransaction (
	IsolationLevel isolevel
)
public function BeginTransaction (
	isolevel : IsolationLevel
) : OdbcTransaction
Not applicable.

Parameters

isolevel

The transaction isolation level for this connection. If you do not specify an isolation level, the default isolation level for the driver is used.

Return Value

An object representing the new transaction.

Exception typeCondition

InvalidOperationException

A transaction is currently active. Parallel transactions are not supported.

To commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.

To make sure that the .NET Framework Data Provider for ODBC transaction management model performs correctly, avoid using other transaction management models, such as those provided by the data source.

The following example creates an OdbcConnection and an OdbcTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.

public static void ExecuteTransaction(string connectionString)
{
    using (OdbcConnection connection =
               new OdbcConnection(connectionString))
    {
        OdbcCommand command = new OdbcCommand();
        OdbcTransaction transaction = null;

        // Set the Connection to the new OdbcConnection.
        command.Connection = connection;

        // Open the connection and execute the transaction.
        try
        {
            connection.Open();

            // Start a local transaction
            transaction = connection.BeginTransaction();

            // Assign transaction object for a pending local transaction.
            command.Connection = connection;
            command.Transaction = transaction;

            // Execute the commands.
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.ExecuteNonQuery();
            command.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
            command.ExecuteNonQuery();

            // Commit the transaction.
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            try
            {
                // Attempt to roll back the transaction.
                transaction.Rollback();
            }
            catch
            {
                // Do nothing here; transaction is not active.
            }
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1

Community Additions

ADD
Show:
© 2014 Microsoft