OdbcTransaction OdbcTransaction OdbcTransaction OdbcTransaction Class

Definizione

Rappresenta una transazione SQL da eseguire in un'origine dati.Represents an SQL transaction to be made at a data source. Questa classe non può essere ereditata.This class cannot be inherited.

public ref class OdbcTransaction sealed : System::Data::Common::DbTransaction
public sealed class OdbcTransaction : System.Data.Common.DbTransaction
type OdbcTransaction = class
    inherit DbTransaction
Public NotInheritable Class OdbcTransaction
Inherits DbTransaction
Ereditarietà

Esempi

Nell'esempio seguente vengono creati OdbcConnection un oggetto OdbcTransactione un oggetto.The following example creates an OdbcConnection and an OdbcTransaction. Viene inoltre illustrato come utilizzare i BeginTransactionmetodi, Commite. RollbackIt 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.
    }
Public Sub ExecuteTransaction(ByVal connectionString As String)

    Using connection As New OdbcConnection(connectionString)
        Dim command As New OdbcCommand()
        Dim transaction As OdbcTransaction

        ' 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 ex As Exception
            Console.WriteLine(ex.Message)
            ' Try to rollback the transaction
            Try
                transaction.Rollback()

            Catch
                ' Do nothing here; transaction is not active.
            End Try
        End Try
        ' The connection is automatically closed when the
        ' code exits the Using block.
    End Using
End Sub

Commenti

L'applicazione crea un OdbcTransaction oggetto chiamando BeginTransaction sull' OdbcConnection oggetto.The application creates an OdbcTransaction object by calling BeginTransaction on the OdbcConnection object. Tutte le operazioni successive associate alla transazione, ad esempio il commit o l'interruzione della transazione, vengono eseguite sull' OdbcTransaction oggetto.All subsequent operations associated with the transaction (for example, committing or aborting the transaction) are performed on the OdbcTransaction object.

Proprietà

Connection Connection Connection Connection

Ottiene l'oggetto OdbcConnection associato alla transazione o null se la transazione non è più valida.Gets the OdbcConnection object associated with the transaction, or null if the transaction is no longer valid.

IsolationLevel IsolationLevel IsolationLevel IsolationLevel

Specifica l'oggetto IsolationLevel per questa transazione.Specifies the IsolationLevel for this transaction.

Metodi

Commit() Commit() Commit() Commit()

Esegue il commit della transazione di database.Commits the database transaction.

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

Consente di creare un oggetto che contiene tutte le informazioni rilevanti necessarie per la generazione del proxy utilizzato per effettuare la comunicazione con un oggetto remoto.Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

Consente di recuperare l'oggetto servizio di durata corrente per controllare i criteri di durata per l'istanza.Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

Ottiene un oggetto servizio di durata per controllare i criteri di durata per questa istanza.Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

Crea una copia dei riferimenti dell'oggetto MarshalByRefObject corrente.Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
Rollback() Rollback() Rollback() Rollback()

Esegue il rollback di una transazione da uno stato in sospeso.Rolls back a transaction from a pending state.

ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Implementazioni dell'interfaccia esplicita

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

Rilascia le risorse usate dall'istanza corrente della classe OdbcTransaction.Releases the resources used by the current instance of the OdbcTransaction class.

Si applica a

Vedi anche