OdbcConnection.BeginTransaction Metod

Definition

Startar en transaktion vid datakällan.

Överlagringar

Name Description
BeginTransaction()

Startar en transaktion vid datakällan.

BeginTransaction(IsolationLevel)

Startar en transaktion vid datakällan med det angivna IsolationLevel värdet.

BeginTransaction()

Källa:
OdbcConnection.cs
Källa:
OdbcConnection.cs
Källa:
OdbcConnection.cs
Källa:
OdbcConnection.cs

Startar en transaktion vid datakällan.

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

Returer

Ett objekt som representerar den nya transaktionen.

Undantag

En transaktion är för närvarande aktiv. Parallella transaktioner stöds inte.

Exempel

I följande exempel skapas en OdbcConnection och en OdbcTransaction. Det visar också hur du använder BeginTransactionmetoderna , Commitoch 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

Kommentarer

Om du vill checka in eller återställa transaktionen måste du uttryckligen Commit använda metoderna eller Rollback .

För att säkerställa att .NET Framework-Data Provider för ODBC-transaktionshanteringsmodellen fungerar korrekt bör du undvika att använda andra transaktionshanteringsmodeller, till exempel de som tillhandahålls av datakällan.

Note

Om du inte anger någon isoleringsnivå bestäms isoleringsnivån av drivrutinen som används. Om du vill ange en isoleringsnivå med BeginTransaction metoden använder du den överlagring som tar parametern isolevel .

Se även

Gäller för

BeginTransaction(IsolationLevel)

Källa:
OdbcConnection.cs
Källa:
OdbcConnection.cs
Källa:
OdbcConnection.cs
Källa:
OdbcConnection.cs

Startar en transaktion vid datakällan med det angivna IsolationLevel värdet.

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

Parametrar

isolevel
IsolationLevel

Transaktionsisoleringsnivån för den här anslutningen. Om du inte anger någon isoleringsnivå används standardisoleringsnivån för drivrutinen.

Returer

Ett objekt som representerar den nya transaktionen.

Undantag

En transaktion är för närvarande aktiv. Parallella transaktioner stöds inte.

Exempel

I följande exempel skapas en OdbcConnection och en OdbcTransaction. Det visar också hur du använder BeginTransactionmetoderna , Commitoch 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

Kommentarer

Om du vill checka in eller återställa transaktionen måste du uttryckligen Commit använda metoderna eller Rollback .

För att säkerställa att .NET Framework-Data Provider för ODBC-transaktionshanteringsmodellen fungerar korrekt bör du undvika att använda andra transaktionshanteringsmodeller, till exempel de som tillhandahålls av datakällan.

Se även

Gäller för