OleDbConnection.BeginTransaction Metodo

Definizione

Avvia una transazione di database.

Overload

BeginTransaction()

Avvia una transazione di database con il valore IsolationLevel corrente.

BeginTransaction(IsolationLevel)

Inizia una transazione di database con il livello di isolamento specificato.

BeginTransaction()

Source:
OleDbConnection.cs
Source:
OleDbConnection.cs
Source:
OleDbConnection.cs

Avvia una transazione di database con il valore IsolationLevel corrente.

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

Restituisce

Oggetto che rappresenta la nuova transazione.

Eccezioni

Le transazioni parallele non sono supportate.

Esempio

Nell'esempio seguente viene creato un OleDbConnection oggetto e un OleDbTransactionoggetto . Illustra anche come usare i BeginTransactionmetodi , Commite Rollback .

public void ExecuteTransaction(string connectionString)
{
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand();
        OleDbTransaction transaction = null;

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the transaction.
        try
        {
            connection.Open();

            // Start a local transaction with ReadCommitted isolation level.
            transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);

            // 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 OleDbConnection(connectionString)
        Dim command As New OleDbCommand()
        Dim transaction As OleDbTransaction

        ' Set the Connection to the new OleDbConnection.
        command.Connection = connection

        ' Open the connection and execute the transaction.
        Try
            connection.Open()

            ' Start a local transaction with ReadCommitted isolation level.
            transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)

            ' 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

Commenti

È necessario eseguire il commit o il rollback esplicito della transazione usando il Commit metodo o Rollback . Per assicurarsi che il modello di gestione delle transazioni DI .NET Framework per OLE DB venga eseguito correttamente, evitare l'uso di altri modelli di gestione delle transazioni, ad esempio quelli forniti dall'origine dati.

Vedi anche

Si applica a

BeginTransaction(IsolationLevel)

Source:
OleDbConnection.cs
Source:
OleDbConnection.cs
Source:
OleDbConnection.cs

Inizia una transazione di database con il livello di isolamento specificato.

public:
 System::Data::OleDb::OleDbTransaction ^ BeginTransaction(System::Data::IsolationLevel isolationLevel);
public System.Data.OleDb.OleDbTransaction BeginTransaction (System.Data.IsolationLevel isolationLevel);
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OleDb.OleDbTransaction
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OleDb.OleDbTransaction
Public Function BeginTransaction (isolationLevel As IsolationLevel) As OleDbTransaction

Parametri

isolationLevel
IsolationLevel

Livello di isolamento in cui eseguire la transazione.

Restituisce

Oggetto che rappresenta la nuova transazione.

Eccezioni

Le transazioni parallele non sono supportate.

Esempio

Nell'esempio seguente viene creato un OleDbConnection oggetto e un OleDbTransactionoggetto . Illustra anche come usare i BeginTransactionmetodi , a Commite Rollback .

public void ExecuteTransaction(string connectionString)
{
    using (OleDbConnection connection =
               new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand();
        OleDbTransaction transaction = null;

        // Set the Connection to the new OleDbConnection.
        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 OleDbConnection(connectionString)
        Dim command As New OleDbCommand()
        Dim transaction As OleDbTransaction

        ' Set the Connection to the new OleDbConnection.
        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

Commenti

È necessario eseguire il commit o il rollback esplicito della transazione usando il Commit metodo o Rollback . Per assicurarsi che il modello di gestione delle transazioni DI .NET Framework per OLE DB venga eseguito correttamente, evitare l'uso di altri modelli di gestione delle transazioni, ad esempio quelli forniti dall'origine dati.

Nota

Se non si specifica un livello di isolamento, viene usato il livello di isolamento predefinito per il provider sottostante. Per specificare un livello di isolamento con il metodo, usare l'overload BeginTransaction che accetta il isolationLevel parametro.

Vedi anche

Si applica a