Aracılığıyla paylaş


OleDbConnection.BeginTransaction Yöntem

Tanım

Bir veritabanı işlemi başlatır.

Aşırı Yüklemeler

BeginTransaction()

Geçerli IsolationLevel değerle bir veritabanı işlemi başlatır.

BeginTransaction(IsolationLevel)

Belirtilen yalıtım düzeyine sahip bir veritabanı işlemi başlatır.

BeginTransaction()

Kaynak:
OleDbConnection.cs
Kaynak:
OleDbConnection.cs
Kaynak:
OleDbConnection.cs

Geçerli IsolationLevel değerle bir veritabanı işlemi başlatır.

public:
 System::Data::OleDb::OleDbTransaction ^ BeginTransaction();
public System.Data.OleDb.OleDbTransaction BeginTransaction ();
override this.BeginTransaction : unit -> System.Data.OleDb.OleDbTransaction
member this.BeginTransaction : unit -> System.Data.OleDb.OleDbTransaction
Public Function BeginTransaction () As OleDbTransaction

Döndürülenler

Yeni işlemi temsil eden bir nesne.

Özel durumlar

Paralel işlemler desteklenmez.

Örnekler

Aşağıdaki örnek bir OleDbConnection ve OleDbTransactionoluşturur. Ayrıca , Commitve Rollback yöntemlerinin BeginTransactionnasıl kullanılacağını da gösterir.

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.
    }
}
Public Sub ExecuteTransaction(ByVal connectionString As String)

    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand()
        Dim transaction As OleDbTransaction

        ' 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 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

Açıklamalar

veya Rollback yöntemini kullanarak Commit işlemi açıkça işlemeniz veya geri almanız gerekir. OLE DB işlem yönetimi modeli için .NET Framework Veri Sağlayıcısı'nın doğru performans gösterdiğinden emin olmak için, veri kaynağı tarafından sağlananlar gibi diğer işlem yönetimi modellerini kullanmaktan kaçının.

Ayrıca bkz.

Şunlara uygulanır

BeginTransaction(IsolationLevel)

Kaynak:
OleDbConnection.cs
Kaynak:
OleDbConnection.cs
Kaynak:
OleDbConnection.cs

Belirtilen yalıtım düzeyine sahip bir veritabanı işlemi başlatır.

public:
 System::Data::OleDb::OleDbTransaction ^ BeginTransaction(System::Data::IsolationLevel isolationLevel);
public System.Data.OleDb.OleDbTransaction BeginTransaction (System.Data.IsolationLevel isolationLevel);
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OleDb.OleDbTransaction
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OleDb.OleDbTransaction
Public Function BeginTransaction (isolationLevel As IsolationLevel) As OleDbTransaction

Parametreler

isolationLevel
IsolationLevel

İşlemin altında çalıştırılması gereken yalıtım düzeyi.

Döndürülenler

Yeni işlemi temsil eden bir nesne.

Özel durumlar

Paralel işlemler desteklenmez.

Örnekler

Aşağıdaki örnek bir OleDbConnection ve OleDbTransactionoluşturur. Ayrıca , ve CommitRollback yöntemlerinin BeginTransactionnasıl kullanılacağını da gösterir.

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.
    }
}
Public Sub ExecuteTransaction(ByVal connectionString As String)

    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand()
        Dim transaction As OleDbTransaction

        ' 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 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

Açıklamalar

veya Rollback yöntemini kullanarak Commit işlemi açıkça işlemeniz veya geri almanız gerekir. OLE DB işlem yönetimi modeli için .NET Framework Veri Sağlayıcısı'nın doğru performans gösterdiğinden emin olmak için, veri kaynağı tarafından sağlananlar gibi diğer işlem yönetimi modellerini kullanmaktan kaçının.

Not

Bir yalıtım düzeyi belirtmezseniz, temel alınan sağlayıcı için varsayılan yalıtım düzeyi kullanılır. yöntemiyle BeginTransaction bir yalıtım düzeyi belirtmek için parametresini alan isolationLevel aşırı yüklemeyi kullanın.

Ayrıca bkz.

Şunlara uygulanır