OdbcTransaction Class

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)

System.Object
  System.MarshalByRefObject
    System.Data.Common.DbTransaction
      System.Data.Odbc.OdbcTransaction

public sealed class OdbcTransaction : DbTransaction

NameDescription
System_CAPS_pubpropertyConnection

Gets the OdbcConnection object associated with the transaction, or null if the transaction is no longer valid.

System_CAPS_pubpropertyIsolationLevel

Specifies the IsolationLevel for this transaction.(Overrides DbTransaction.IsolationLevel.)

NameDescription
System_CAPS_pubmethodCommit()

Commits the database transaction.(Overrides DbTransaction.Commit().)

System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDispose()

Releases the unmanaged resources used by the DbTransaction.(Inherited from DbTransaction.)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodRollback()

Rolls back a transaction from a pending state.(Overrides DbTransaction.Rollback().)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privpropertyIDbTransaction.Connection

Gets the DbConnection object associated with the transaction, or a null reference if the transaction is no longer valid.(Inherited from 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.
    }

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft