Bagikan melalui


OdbcConnection.BeginTransaction Metode

Definisi

Memulai transaksi di sumber data.

Overload

BeginTransaction()

Memulai transaksi di sumber data.

BeginTransaction(IsolationLevel)

Memulai transaksi di sumber data dengan nilai yang ditentukan IsolationLevel .

BeginTransaction()

Sumber:
OdbcConnection.cs
Sumber:
OdbcConnection.cs

Memulai transaksi di sumber data.

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

Mengembalikan

Objek yang mewakili transaksi baru.

Pengecualian

Transaksi saat ini aktif. Transaksi paralel tidak didukung.

Contoh

Contoh berikut membuat OdbcConnection dan OdbcTransaction. Ini juga menunjukkan cara menggunakan BeginTransactionmetode , , Commitdan Rollback .

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

Keterangan

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

Untuk memastikan bahwa .NET Framework Data Provider untuk model manajemen transaksi ODBC berfungsi dengan benar, hindari menggunakan model manajemen transaksi lainnya, seperti yang disediakan oleh sumber data.

Catatan

Jika Anda tidak menentukan tingkat isolasi, tingkat isolasi akan ditentukan oleh driver yang digunakan. Untuk menentukan tingkat isolasi dengan BeginTransaction metode , gunakan kelebihan beban yang mengambil isolevel parameter .

Lihat juga

Berlaku untuk

BeginTransaction(IsolationLevel)

Sumber:
OdbcConnection.cs
Sumber:
OdbcConnection.cs

Memulai transaksi di sumber data dengan nilai yang ditentukan IsolationLevel .

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
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.Odbc.OdbcTransaction
Public Function BeginTransaction (isolevel As IsolationLevel) As OdbcTransaction

Parameter

isolevel
IsolationLevel

Tingkat isolasi transaksi untuk koneksi ini. Jika Anda tidak menentukan tingkat isolasi, tingkat isolasi default untuk driver akan digunakan.

Mengembalikan

Objek yang mewakili transaksi baru.

Pengecualian

Transaksi saat ini aktif. Transaksi paralel tidak didukung.

Contoh

Contoh berikut membuat OdbcConnection dan OdbcTransaction. Ini juga menunjukkan cara menggunakan BeginTransactionmetode , , Commitdan Rollback .

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

Keterangan

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

Untuk memastikan bahwa .NET Framework Data Provider untuk model manajemen transaksi ODBC berfungsi dengan benar, hindari menggunakan model manajemen transaksi lainnya, seperti yang disediakan oleh sumber data.

Lihat juga

Berlaku untuk