BeginTransaction Method
Collapse the table of content
Expand the table of content

OdbcConnection.BeginTransaction Method ()


Starts a transaction at the data source.

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

public OdbcTransaction BeginTransaction()

Return Value

Type: System.Data.Odbc.OdbcTransaction

An object representing the new transaction.

Exception Condition

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.


If you do not specify an isolation level, the isolation level will be determined by the driver being used. To specify an isolation level with the BeginTransaction method, use the overload that takes the isolevel parameter.

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.

            // 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.CommandText =
                "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";

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

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft