OdbcTransaction.Rollback Méthode

Définition

Annule une transaction à partir d’un état d’attente.Rolls back a transaction from a pending state.

public:
 virtual void Rollback();
public void Rollback ();
override this.Rollback : unit -> unit
Public Sub Rollback ()

Exceptions

Une erreur s’est produite lors de la tentative de validation de la transaction.An error occurred while trying to commit the transaction.

La transaction a déjà été validée ou annulée.The transaction has already been committed or rolled back.

- ou --or-

La connexion est interrompue.The connection is broken.

Exemples

L’exemple suivant crée un OdbcConnection et un OdbcTransaction.The following example creates an OdbcConnection and an OdbcTransaction. Il montre également comment utiliser les BeginTransactionméthodes, Commitet. 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

Remarques

La transaction ne peut être restaurée qu’à partir d’un état d' BeginTransaction attente (après l’appel de Commit , mais avant l’appel de).The transaction can be rolled back only from a pending state (after BeginTransaction has been called, but before Commit is called). La transaction est restaurée dans le cas où elle est supprimée avant Commit ou Rollback appelée.The transaction will be rolled back in the event it is disposed before Commit or Rollback is called.

S’applique à

Voir aussi