OleDbConnection.BeginTransaction 方法

定义

开始数据库事务。

重载

BeginTransaction()

以当前的 IsolationLevel 值开始数据库事务。

BeginTransaction(IsolationLevel)

以指定的隔离级别启动数据库事务。

BeginTransaction()

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

以当前的 IsolationLevel 值开始数据库事务。

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

返回

表示新事务的对象。

例外

不支持并行事务。

示例

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

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

注解

必须使用 或 Rollback 方法显式提交或回滚事务Commit。 若要确保 .NET Framework OLE DB 事务管理模型的数据提供程序正确执行,请避免使用其他事务管理模型,例如数据源提供的事务管理模型。

另请参阅

适用于

BeginTransaction(IsolationLevel)

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

以指定的隔离级别启动数据库事务。

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

参数

isolationLevel
IsolationLevel

事务应在其下运行的隔离级别。

返回

表示新事务的对象。

例外

不支持并行事务。

示例

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

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

注解

必须使用 或 Rollback 方法显式提交或回滚事务Commit。 若要确保 .NET Framework OLE DB 事务管理模型的数据提供程序正确执行,请避免使用其他事务管理模型,例如数据源提供的事务管理模型。

注意

如果未指定隔离级别,则使用基础提供程序的默认隔离级别。 若要使用 BeginTransaction 方法指定隔离级别,请使用采用 参数的 isolationLevel 重载。

另请参阅

适用于