Przeczytaj w języku angielskim

Udostępnij za pośrednictwem


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.

C#
public System.Data.Odbc.OdbcTransaction BeginTransaction();

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 .

C#
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.
    }
}

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

.NET 10 (package-provided) i inne wersje
Produkt Wersje
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

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ą.

C#
public System.Data.Odbc.OdbcTransaction BeginTransaction(System.Data.IsolationLevel isolevel);

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 .

C#
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.
    }
}

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

.NET 10 (package-provided) i inne wersje
Produkt Wersje
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)