OdbcConnection.BeginTransaction Metoda

Definicja

Uruchamia transakcję w źródle danych.

Przeciążenia

BeginTransaction()

Uruchamia transakcję w źródle danych.

BeginTransaction(IsolationLevel)

Uruchamia transakcję w źródle danych z określoną IsolationLevel wartością.

BeginTransaction()

Źródło:
OdbcConnection.cs
Źródło:
OdbcConnection.cs
Źródło:
OdbcConnection.cs

Uruchamia transakcję w źródle danych.

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

Zwraca

Obiekt reprezentujący nową transakcję.

Wyjątki

Transakcja jest obecnie aktywna. Transakcje równoległe nie są obsługiwane.

Przykłady

Poniższy przykład tworzy obiekt OdbcConnection i OdbcTransaction. Demonstruje również sposób używania BeginTransactionmetod , Commiti 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

Uwagi

Aby zatwierdzić lub wycofać transakcję, należy jawnie użyć Commit metod lub Rollback .

Aby upewnić się, że dostawca danych .NET Framework dla modelu zarządzania transakcjami ODBC działa prawidłowo, należy unikać używania innych modeli zarządzania transakcjami, takich jak te udostępniane przez źródło danych.

Uwaga

Jeśli nie określisz poziomu izolacji, poziom izolacji zostanie określony przez używany sterownik. Aby określić poziom izolacji za BeginTransaction pomocą metody , użyj przeciążenia, które przyjmuje isolevel parametr .

Zobacz też

Dotyczy

BeginTransaction(IsolationLevel)

Źródło:
OdbcConnection.cs
Źródło:
OdbcConnection.cs
Źródło:
OdbcConnection.cs

Uruchamia transakcję w źródle danych z określoną IsolationLevel wartością.

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

Parametry

isolevel
IsolationLevel

Poziom izolacji transakcji dla tego połączenia. Jeśli nie określisz poziomu izolacji, zostanie użyty domyślny poziom izolacji dla sterownika.

Zwraca

Obiekt reprezentujący nową transakcję.

Wyjątki

Transakcja jest obecnie aktywna. Transakcje równoległe nie są obsługiwane.

Przykłady

Poniższy przykład tworzy obiekt OdbcConnection i OdbcTransaction. Demonstruje również sposób używania BeginTransactionmetod , Commiti 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

Uwagi

Aby zatwierdzić lub wycofać transakcję, należy jawnie użyć Commit metod lub Rollback .

Aby upewnić się, że dostawca danych .NET Framework dla modelu zarządzania transakcjami ODBC działa prawidłowo, należy unikać używania innych modeli zarządzania transakcjami, takich jak te udostępniane przez źródło danych.

Zobacz też

Dotyczy