Compartir a través de


OracleConnection.BeginTransaction Método

Definición

Inicia una transacción en la base de datos.

Sobrecargas

BeginTransaction()

Inicia una transacción en la base de datos.

BeginTransaction(IsolationLevel)

Inicia una transacción en la base de datos con el valor de IsolationLevel especificado.

BeginTransaction()

Inicia una transacción en la base de datos.

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

Devoluciones

Objeto que representa la nueva transacción.

Excepciones

No se admiten transacciones paralelas.

Ejemplos

En el ejemplo siguiente se crea un OracleConnection objeto y .OracleTransaction También muestra cómo usar los BeginTransactionmétodos , Commity Rollback .

public void RunOracleTransaction(string connectionString)
{
    using (OracleConnection connection = new OracleConnection(connectionString))
    {
        connection.Open();

        OracleCommand command = connection.CreateCommand();
        OracleTransaction transaction;

        // Start a local transaction
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
        // Assign transaction object for a pending local transaction
        command.Transaction = transaction;

        try
        {
            command.CommandText =
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
            command.ExecuteNonQuery();
            command.CommandText =
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
            command.ExecuteNonQuery();
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception e)
        {
            transaction.Rollback();
            Console.WriteLine(e.ToString());
            Console.WriteLine("Neither record was written to database.");
        }
    }
}
Public Sub RunOracleTransaction(ByVal connectionString As String)
    Using connection As New OracleConnection(connectionString)
        connection.Open()

        Dim command As OracleCommand = connection.CreateCommand()
        Dim transaction As OracleTransaction

        ' Start a local transaction
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
        ' Assign transaction object for a pending local transaction
        command.Transaction = transaction

        Try
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')"
            command.ExecuteNonQuery()
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')"
            command.ExecuteNonQuery()
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")
        Catch e As Exception
            transaction.Rollback()
            Console.WriteLine(e.ToString())
            Console.WriteLine("Neither record was written to database.")
        End Try
    End Using
End Sub

Comentarios

Para confirmar o revertir la transacción, debe usar explícitamente los Commit métodos o Rollback .

Para asegurarse de que el modelo de administración de transacciones de .NET Framework para Oracle funciona correctamente, evite usar otros modelos de administración de transacciones, como los proporcionados por la base de datos.

Nota

Si no especifica un nivel de aislamiento, se usa el nivel de aislamiento predeterminado. Para especificar un nivel de aislamiento con el BeginTransaction método , use la BeginTransaction sobrecarga .

Consulte también

Se aplica a

BeginTransaction(IsolationLevel)

Inicia una transacción en la base de datos con el valor de IsolationLevel especificado.

public:
 System::Data::OracleClient::OracleTransaction ^ BeginTransaction(System::Data::IsolationLevel il);
public System.Data.OracleClient.OracleTransaction BeginTransaction (System.Data.IsolationLevel il);
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OracleClient.OracleTransaction
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.OracleClient.OracleTransaction
Public Function BeginTransaction (il As IsolationLevel) As OracleTransaction

Parámetros

il
IsolationLevel

Nivel de aislamiento de transacción de esta conexión.

Devoluciones

Objeto que representa la nueva transacción.

Excepciones

No se admiten transacciones paralelas.

Ejemplos

En el ejemplo siguiente se crea un OracleConnection objeto y .OracleTransaction También muestra cómo usar los BeginTransactionmétodos , Commity Rollback .

public void RunOracleTransaction(string connectionString)
{
    using (OracleConnection connection = new OracleConnection(connectionString))
    {
        connection.Open();

        OracleCommand command = connection.CreateCommand();
        OracleTransaction transaction;

        // Start a local transaction
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
        // Assign transaction object for a pending local transaction
        command.Transaction = transaction;

        try
        {
            command.CommandText =
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')";
            command.ExecuteNonQuery();
            command.CommandText =
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')";
            command.ExecuteNonQuery();
            transaction.Commit();
            Console.WriteLine("Both records are written to database.");
        }
        catch (Exception e)
        {
            transaction.Rollback();
            Console.WriteLine(e.ToString());
            Console.WriteLine("Neither record was written to database.");
        }
    }
}
Public Sub RunOracleTransaction(ByVal connectionString As String)
    Using connection As New OracleConnection(connectionString)
        connection.Open()

        Dim command As OracleCommand = connection.CreateCommand()
        Dim transaction As OracleTransaction

        ' Start a local transaction
        transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
        ' Assign transaction object for a pending local transaction
        command.Transaction = transaction

        Try
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (50, 'TECHNOLOGY', 'DENVER')"
            command.ExecuteNonQuery()
            command.CommandText = _
                "INSERT INTO Dept (DeptNo, Dname, Loc) values (60, 'ENGINEERING', 'KANSAS CITY')"
            command.ExecuteNonQuery()
            transaction.Commit()
            Console.WriteLine("Both records are written to database.")
        Catch e As Exception
            transaction.Rollback()
            Console.WriteLine(e.ToString())
            Console.WriteLine("Neither record was written to database.")
        End Try
    End Using
End Sub

Comentarios

Para confirmar o revertir la transacción, debe usar explícitamente los Commit métodos o Rollback .

Para asegurarse de que el modelo de administración de transacciones de .NET Framework para Oracle funciona correctamente, evite usar otros modelos de administración de transacciones, como los proporcionados por la base de datos.

Consulte también

Se aplica a