Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

OdbcTransaction Class

Represents an SQL transaction to be made at a data source. This class cannot be inherited.

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

public sealed class OdbcTransaction : DbTransaction
public final class OdbcTransaction extends DbTransaction
public final class OdbcTransaction extends DbTransaction

The application creates an OdbcTransaction object by calling BeginTransaction on the OdbcConnection object. All subsequent operations associated with the transaction (for example, committing or aborting the transaction) are performed on the OdbcTransaction object.

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.
    

System.Object
   System.MarshalByRefObject
     System.Data.Common.DbTransaction
      System.Data.Odbc.OdbcTransaction
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.