Bagikan melalui


OracleConnection.BeginTransaction Metode

Definisi

Memulai transaksi di database.

Overload

BeginTransaction()

Memulai transaksi di database.

BeginTransaction(IsolationLevel)

Memulai transaksi di database dengan nilai yang ditentukan IsolationLevel .

BeginTransaction()

Memulai transaksi di database.

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

Mengembalikan

Objek yang mewakili transaksi baru.

Pengecualian

Transaksi paralel tidak didukung.

Contoh

Contoh berikut membuat OracleConnection dan OracleTransaction. Ini juga menunjukkan cara menggunakan BeginTransactionmetode , Commit, dan Rollback .

public void RunOracleTransaction(string connectionString)
{
    using (OracleConnection connection = new OracleConnection(connectionString))
    {
        connection.Open();

        OracleCommand command = connection.CreateCommand();
        OracleTransaction transaction;

        // Start a local transaction
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
        // Assign transaction object for a pending local transaction
        command.Transaction = transaction;

        try
        {
            command.CommandText =
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
            command.ExecuteNonQuery();
            command.CommandText =
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
            command.ExecuteNonQuery();
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception e)
        {
            transaction.Rollback();
            Console.WriteLine(e.ToString());
            Console.WriteLine("Neither record was written to database.");
        }
    }
}
Public Sub RunOracleTransaction(ByVal connectionString As String)
    Using connection As New OracleConnection(connectionString)
        connection.Open()

        Dim command As OracleCommand = connection.CreateCommand()
        Dim transaction As OracleTransaction

        ' Start a local transaction
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
        ' Assign transaction object for a pending local transaction
        command.Transaction = transaction

        Try
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')"
            command.ExecuteNonQuery()
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')"
            command.ExecuteNonQuery()
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")
        Catch e As Exception
            transaction.Rollback()
            Console.WriteLine(e.ToString())
            Console.WriteLine("Neither record was written to database.")
        End Try
    End Using
End Sub

Keterangan

Untuk menerapkan atau mengembalikan transaksi, Anda harus secara eksplisit menggunakan Commit metode atau Rollback .

Untuk memastikan bahwa model manajemen transaksi Penyedia Data .NET Framework untuk Oracle berkinerja dengan benar, hindari menggunakan model manajemen transaksi lainnya, seperti yang disediakan oleh database.

Catatan

Jika Anda tidak menentukan tingkat isolasi, tingkat isolasi default akan digunakan. Untuk menentukan tingkat isolasi dengan BeginTransaction metode , gunakan BeginTransaction kelebihan beban.

Lihat juga

Berlaku untuk

BeginTransaction(IsolationLevel)

Memulai transaksi di database dengan nilai yang ditentukan IsolationLevel .

public:
 System::Data::OracleClient::OracleTransaction ^ BeginTransaction(System::Data::IsolationLevel il);
public System.Data.OracleClient.OracleTransaction BeginTransaction (System.Data.IsolationLevel il);
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OracleClient.OracleTransaction
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OracleClient.OracleTransaction
Public Function BeginTransaction (il As IsolationLevel) As OracleTransaction

Parameter

il
IsolationLevel

Tingkat isolasi transaksi untuk koneksi ini.

Mengembalikan

Objek yang mewakili transaksi baru.

Pengecualian

Transaksi paralel tidak didukung.

Contoh

Contoh berikut membuat OracleConnection dan OracleTransaction. Ini juga menunjukkan cara menggunakan BeginTransactionmetode , Commit, dan Rollback .

public void RunOracleTransaction(string connectionString)
{
    using (OracleConnection connection = new OracleConnection(connectionString))
    {
        connection.Open();

        OracleCommand command = connection.CreateCommand();
        OracleTransaction transaction;

        // Start a local transaction
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
        // Assign transaction object for a pending local transaction
        command.Transaction = transaction;

        try
        {
            command.CommandText =
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
            command.ExecuteNonQuery();
            command.CommandText =
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
            command.ExecuteNonQuery();
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception e)
        {
            transaction.Rollback();
            Console.WriteLine(e.ToString());
            Console.WriteLine("Neither record was written to database.");
        }
    }
}
Public Sub RunOracleTransaction(ByVal connectionString As String)
    Using connection As New OracleConnection(connectionString)
        connection.Open()

        Dim command As OracleCommand = connection.CreateCommand()
        Dim transaction As OracleTransaction

        ' Start a local transaction
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
        ' Assign transaction object for a pending local transaction
        command.Transaction = transaction

        Try
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')"
            command.ExecuteNonQuery()
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')"
            command.ExecuteNonQuery()
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")
        Catch e As Exception
            transaction.Rollback()
            Console.WriteLine(e.ToString())
            Console.WriteLine("Neither record was written to database.")
        End Try
    End Using
End Sub

Keterangan

Untuk menerapkan atau mengembalikan transaksi, Anda harus secara eksplisit menggunakan Commit metode atau Rollback .

Untuk memastikan bahwa model manajemen transaksi Penyedia Data .NET Framework untuk Oracle berkinerja dengan benar, hindari menggunakan model manajemen transaksi lainnya, seperti yang disediakan oleh database.

Lihat juga

Berlaku untuk