OdbcConnection.BeginTransaction OdbcConnection.BeginTransaction OdbcConnection.BeginTransaction OdbcConnection.BeginTransaction Method

Définition

Commence une transaction dans la source de données.Starts a transaction at the data source.

Surcharges

BeginTransaction() BeginTransaction() BeginTransaction() BeginTransaction()

Commence une transaction dans la source de données.Starts a transaction at the data source.

BeginTransaction(IsolationLevel) BeginTransaction(IsolationLevel) BeginTransaction(IsolationLevel) BeginTransaction(IsolationLevel)

Commence une transaction dans la source de données avec la valeur IsolationLevel spécifiée.Starts a transaction at the data source with the specified IsolationLevel value.

BeginTransaction() BeginTransaction() BeginTransaction() BeginTransaction()

Commence une transaction dans la source de données.Starts a transaction at the data source.

public:
 System::Data::Odbc::OdbcTransaction ^ BeginTransaction();
public System.Data.Odbc.OdbcTransaction BeginTransaction ();
override this.BeginTransaction : unit -> System.Data.Odbc.OdbcTransaction
Public Function BeginTransaction () As OdbcTransaction

Retours

Objet représentant la nouvelle transaction.An object representing the new transaction.

Exceptions

Une transaction est active.A transaction is currently active. Les transactions parallèles ne sont pas prises en charge.Parallel transactions are not supported.

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

Pour valider ou restaurer la transaction, vous devez utiliser explicitement les Commit méthodes ou. RollbackTo commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.

Pour vous assurer que le .NET Framework Fournisseur de données le modèle de gestion des transactions ODBC fonctionne correctement, évitez d’utiliser d’autres modèles de gestion des transactions, tels que ceux fournis par la source de données.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.

Notes

Si vous ne spécifiez pas de niveau d’isolation, le niveau d’isolation sera déterminé par le pilote utilisé.If you do not specify an isolation level, the isolation level will be determined by the driver being used. Pour spécifier un niveau d’isolation à BeginTransaction l’aide de la méthode, utilisez la isolevel surcharge qui prend le paramètre.To specify an isolation level with the BeginTransaction method, use the overload that takes the isolevel parameter.

Voir aussi

BeginTransaction(IsolationLevel) BeginTransaction(IsolationLevel) BeginTransaction(IsolationLevel) BeginTransaction(IsolationLevel)

Commence une transaction dans la source de données avec la valeur IsolationLevel spécifiée.Starts a transaction at the data source with the specified IsolationLevel value.

public:
 System::Data::Odbc::OdbcTransaction ^ BeginTransaction(System::Data::IsolationLevel isolevel);
public System.Data.Odbc.OdbcTransaction BeginTransaction (System.Data.IsolationLevel isolevel);
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.Odbc.OdbcTransaction
Public Function BeginTransaction (isolevel As IsolationLevel) As OdbcTransaction

Paramètres

isolevel
IsolationLevel IsolationLevel IsolationLevel IsolationLevel

Niveau d'isolement des transactions pour cette connexion.The transaction isolation level for this connection. Si vous ne spécifiez pas un niveau d'isolement, le niveau d'isolement par défaut pour le pilote est utilisé.If you do not specify an isolation level, the default isolation level for the driver is used.

Retours

Objet représentant la nouvelle transaction.An object representing the new transaction.

Exceptions

Une transaction est active.A transaction is currently active. Les transactions parallèles ne sont pas prises en charge.Parallel transactions are not supported.

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

Pour valider ou restaurer la transaction, vous devez utiliser explicitement les Commit méthodes ou. RollbackTo commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.

Pour vous assurer que le .NET Framework Fournisseur de données le modèle de gestion des transactions ODBC fonctionne correctement, évitez d’utiliser d’autres modèles de gestion des transactions, tels que ceux fournis par la source de données.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.

Voir aussi

S’applique à