Auf Englisch lesen

Teilen über


OleDbConnection.BeginTransaction Methode

Definition

Startet eine Datenbanktransaktion.

Überlädt

BeginTransaction()

Beginnt eine Datenbanktransaktion mit dem aktuellen IsolationLevel-Wert.

BeginTransaction(IsolationLevel)

Startet eine Datenbanktransaktion mit dem angegebenen Isolationsgrad.

BeginTransaction()

Quelle:
OleDbConnection.cs
Quelle:
OleDbConnection.cs
Quelle:
OleDbConnection.cs

Beginnt eine Datenbanktransaktion mit dem aktuellen IsolationLevel-Wert.

C#
public System.Data.OleDb.OleDbTransaction BeginTransaction ();

Gibt zurück

Ein Objekt, das die neue Transaktion darstellt.

Ausnahmen

Parallele Transaktionen werden nicht unterstützt.

Beispiele

Das folgende Beispiel erstellt eine OleDbConnection und ein OleDbTransaction. Außerdem wird veranschaulicht, wie die BeginTransactionMethoden , Commitund Rollback verwendet werden.

C#
public void ExecuteTransaction(string connectionString)
{
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand();
        OleDbTransaction transaction = null;

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the transaction.
        try
        {
            connection.Open();

            // Start a local transaction with ReadCommitted isolation level.
            transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);

            // 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.
    }
}

Hinweise

Sie müssen die Transaktion explizit mit der Commit -Methode oder committen oder Rollback zurücksetzen. Um sicherzustellen, dass das .NET Framework-Datenanbieter für OLE DB-Transaktionsverwaltungsmodell ordnungsgemäß funktioniert, vermeiden Sie die Verwendung anderer Transaktionsverwaltungsmodelle, z. B. der von der Datenquelle bereitgestellten.

Weitere Informationen

Gilt für:

.NET Framework 4.8.1 und andere Versionen
Produkt Versionen
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

BeginTransaction(IsolationLevel)

Quelle:
OleDbConnection.cs
Quelle:
OleDbConnection.cs
Quelle:
OleDbConnection.cs

Startet eine Datenbanktransaktion mit dem angegebenen Isolationsgrad.

C#
public System.Data.OleDb.OleDbTransaction BeginTransaction (System.Data.IsolationLevel isolationLevel);

Parameter

isolationLevel
IsolationLevel

Die Isolationsstufe, unter der die Transaktion ausgeführt werden soll.

Gibt zurück

Ein Objekt, das die neue Transaktion darstellt.

Ausnahmen

Parallele Transaktionen werden nicht unterstützt.

Beispiele

Das folgende Beispiel erstellt eine OleDbConnection und ein OleDbTransaction. Außerdem wird veranschaulicht, wie die BeginTransactionMethoden , a Commitund Rollback verwendet werden.

C#
public void ExecuteTransaction(string connectionString)
{
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand();
        OleDbTransaction transaction = null;

        // Set the Connection to the new OleDbConnection.
        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.
    }
}

Hinweise

Sie müssen die Transaktion explizit mit der Commit -Methode oder committen oder Rollback zurücksetzen. Um sicherzustellen, dass das .NET Framework-Datenanbieter für OLE DB-Transaktionsverwaltungsmodell ordnungsgemäß funktioniert, vermeiden Sie die Verwendung anderer Transaktionsverwaltungsmodelle, z. B. der von der Datenquelle bereitgestellten.

Hinweis

Wenn Sie keine Isolationsstufe angeben, wird die Standardisolationsstufe für den zugrunde liegenden Anbieter verwendet. Um eine Isolationsstufe mit der BeginTransaction -Methode anzugeben, verwenden Sie die Überladung, die den isolationLevel Parameter akzeptiert.

Weitere Informationen

Gilt für:

.NET Framework 4.8.1 und andere Versionen
Produkt Versionen
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1