OleDbConnection.BeginTransaction メソッド

定義

データベース トランザクションを開始します。Starts a database transaction.

オーバーロード

BeginTransaction()

現在の IsolationLevel 値を使用して、データベース トランザクションを開始します。Starts a database transaction with the current IsolationLevel value.

BeginTransaction(IsolationLevel)

分離レベルを指定して、データベース トランザクションを開始します。Starts a database transaction with the specified isolation level.

BeginTransaction()

現在の IsolationLevel 値を使用して、データベース トランザクションを開始します。Starts a database transaction with the current IsolationLevel value.

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

戻り値

新しいトランザクションを表すオブジェクト。An object representing the new transaction.

例外

並列トランザクションはサポートされていません。Parallel transactions are not supported.

次の例では、OleDbConnectionOleDbTransactionを作成します。The following example creates an OleDbConnection and an OleDbTransaction. また、BeginTransactionCommit、および Rollback メソッドの使用方法も示します。It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods.

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

注釈

Commit または Rollback メソッドを使用して、トランザクションを明示的にコミットまたはロールバックする必要があります。You must explicitly commit or roll back the transaction using the Commit or Rollback method. OLE DB トランザクション管理モデルの .NET Framework Data Provider が正しく動作することを確認するには、データソースによって提供されるものなど、他のトランザクション管理モデルを使用しないようにします。To make sure that the .NET Framework Data Provider for OLE DB transaction management model performs correctly, avoid using other transaction management models, such as those provided by the data source.

こちらもご覧ください

BeginTransaction(IsolationLevel)

分離レベルを指定して、データベース トランザクションを開始します。Starts a database transaction with the specified isolation level.

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

パラメーター

isolationLevel
IsolationLevel

トランザクションを実行する分離レベル。The isolation level under which the transaction should run.

戻り値

新しいトランザクションを表すオブジェクト。An object representing the new transaction.

例外

並列トランザクションはサポートされていません。Parallel transactions are not supported.

次の例では、OleDbConnectionOleDbTransactionを作成します。The following example creates an OleDbConnection and an OleDbTransaction. また、BeginTransactionCommit、および Rollback メソッドの使用方法も示します。It also demonstrates how to use the BeginTransaction, a Commit, and Rollback methods.

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

注釈

Commit または Rollback メソッドを使用して、トランザクションを明示的にコミットまたはロールバックする必要があります。You must explicitly commit or roll back the transaction using the Commit or Rollback method. OLE DB トランザクション管理モデルの .NET Framework Data Provider が正しく動作することを確認するには、データソースによって提供されるものなど、他のトランザクション管理モデルを使用しないようにします。To make sure that the .NET Framework Data Provider for OLE DB transaction management model performs correctly, avoid using other transaction management models, such as those provided by the data source.

注意

分離レベルを指定しない場合は、基になるプロバイダーの既定の分離レベルが使用されます。If you do not specify an isolation level, the default isolation level for the underlying provider is used. BeginTransaction メソッドを使用して分離レベルを指定するには、isolationLevel パラメーターを受け取るオーバーロードを使用します。To specify an isolation level with the BeginTransaction method, use the overload that takes the isolationLevel parameter.

こちらもご覧ください

適用対象