OdbcConnection.BeginTransaction Methode

Definition

Startet eine Transaktion an der Datenquelle.Starts a transaction at the data source.

Überlädt

BeginTransaction()

Startet eine Transaktion an der Datenquelle.Starts a transaction at the data source.

BeginTransaction(IsolationLevel)

Startet eine Transaktion an der Datenquelle mit dem angegebenen IsolationLevel-Wert.Starts a transaction at the data source with the specified IsolationLevel value.

BeginTransaction()

Startet eine Transaktion an der Datenquelle.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

Gibt zurück

Ein Objekt, das die neue Transaktion darstellt.An object representing the new transaction.

Ausnahmen

Eine Transaktion ist gegenwärtig aktiv.A transaction is currently active. Parallele Transaktionen werden nicht unterstützt.Parallel transactions are not supported.

Beispiele

Das folgende Beispiel erstellt eine OdbcConnection und ein OdbcTransaction.The following example creates an OdbcConnection and an OdbcTransaction. Außerdem wird veranschaulicht, wie die Methoden BeginTransaction, Commitund Rollback verwendet werden.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.
    }
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

Hinweise

Zum Ausführen eines Commits oder Rollbacks für die Transaktion müssen Sie explizit die Methoden Commit oder Rollback verwenden.To commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.

Um sicherzustellen, dass die .NET Framework Datenanbieter für das ODBC-Transaktions Verwaltungsmodell ordnungsgemäß ausgeführt wird, vermeiden Sie die Verwendung anderer Transaktions Verwaltungsmodelle, wie z. b. die von der Datenquelle bereitgestellten.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.

Hinweis

Wenn Sie keine Isolationsstufe angeben, wird die Isolationsstufe durch den verwendeten Treiber bestimmt.If you do not specify an isolation level, the isolation level will be determined by the driver being used. Verwenden Sie die-Überladung, die den isolevel-Parameter annimmt, um eine Isolationsstufe mit der BeginTransaction-Methode anzugeben.To specify an isolation level with the BeginTransaction method, use the overload that takes the isolevel parameter.

Siehe auch

BeginTransaction(IsolationLevel)

Startet eine Transaktion an der Datenquelle mit dem angegebenen IsolationLevel-Wert.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

Parameter

isolevel
IsolationLevel

Der Transaktionsisolationsgrad für diese Verbindung.The transaction isolation level for this connection. Wenn Sie keinen Isolationsgrad angeben, wird der Standardisolationsgrad für den Treiber verwendet.If you do not specify an isolation level, the default isolation level for the driver is used.

Gibt zurück

Ein Objekt, das die neue Transaktion darstellt.An object representing the new transaction.

Ausnahmen

Eine Transaktion ist gegenwärtig aktiv.A transaction is currently active. Parallele Transaktionen werden nicht unterstützt.Parallel transactions are not supported.

Beispiele

Das folgende Beispiel erstellt eine OdbcConnection und ein OdbcTransaction.The following example creates an OdbcConnection and an OdbcTransaction. Außerdem wird veranschaulicht, wie die Methoden BeginTransaction, Commitund Rollback verwendet werden.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.
    }
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

Hinweise

Zum Ausführen eines Commits oder Rollbacks für die Transaktion müssen Sie explizit die Methoden Commit oder Rollback verwenden.To commit or roll back the transaction, you must explicitly use the Commit or Rollback methods.

Um sicherzustellen, dass die .NET Framework Datenanbieter für das ODBC-Transaktions Verwaltungsmodell ordnungsgemäß ausgeführt wird, vermeiden Sie die Verwendung anderer Transaktions Verwaltungsmodelle, wie z. b. die von der Datenquelle bereitgestellten.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.

Siehe auch

Gilt für: