OracleConnection.BeginTransaction 方法

定义

在数据库中开始一个事务。

重载

BeginTransaction()

在数据库中开始一个事务。

BeginTransaction(IsolationLevel)

使用指定的 IsolationLevel 值在数据库中开始一个事务。

BeginTransaction()

在数据库中开始一个事务。

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

返回

表示新事务的对象。

例外

不支持并行事务。

示例

以下示例创建 OracleConnectionOracleTransaction。 它还演示如何使用 BeginTransactionCommitRollback 方法。

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

注解

若要提交或回滚事务,必须显式使用 CommitRollback 方法。

若要确保 .NET Framework Oracle 数据提供程序事务管理模型正确执行,请避免使用其他事务管理模型,例如数据库提供的事务管理模型。

注意

如果未指定隔离级别,则使用默认隔离级别。 若要使用 BeginTransaction 方法指定隔离级别,请使用 BeginTransaction 重载。

另请参阅

适用于

BeginTransaction(IsolationLevel)

使用指定的 IsolationLevel 值在数据库中开始一个事务。

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

参数

il
IsolationLevel

该连接的事务隔离级别。

返回

表示新事务的对象。

例外

不支持并行事务。

示例

以下示例创建 OracleConnectionOracleTransaction。 它还演示如何使用 BeginTransactionCommitRollback 方法。

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

注解

若要提交或回滚事务,必须显式使用 CommitRollback 方法。

若要确保 .NET Framework Oracle 数据提供程序事务管理模型正确执行,请避免使用其他事务管理模型,例如数据库提供的事务管理模型。

另请参阅

适用于