OdbcConnection.BeginTransaction メソッド

定義

データ ソースでトランザクションを開始します。

オーバーロード

BeginTransaction()

データ ソースでトランザクションを開始します。

BeginTransaction(IsolationLevel)

指定した IsolationLevel 値を使用して、データ ソースでトランザクションを開始します。

BeginTransaction()

データ ソースでトランザクションを開始します。

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

戻り値

OdbcTransaction

新しいトランザクションを表すオブジェクト。

例外

トランザクションは、現在アクティブです。 並列トランザクションはサポートされていません。

次の例では、OdbcConnection.OdbcTransaction また、メソッドの使用方法BeginTransactionCommitRollbackについても説明します。

public static void ExecuteTransaction(string connectionString)
{
    using (OdbcConnection connection =
               new OdbcConnection(connectionString))
    {
        OdbcCommand command = new OdbcCommand();
        OdbcTransaction transaction = null;

        // Set the Connection to the new OdbcConnection.
        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 OdbcConnection(connectionString)
        Dim command As New OdbcCommand()
        Dim transaction As OdbcTransaction

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

注釈

トランザクションをコミットまたはロールバックするには、or メソッドを明示的に使用するCommitRollback必要があります。

ODBC トランザクション管理モデルの.NET Framework Data Providerが正しく実行されるようにするには、データ ソースによって提供されるトランザクション管理モデルなど、他のトランザクション管理モデルを使用しないようにします。

注意

分離レベルを指定しない場合、分離レベルは、使用されているドライバーによって決定されます。 メソッドで分離レベルを BeginTransaction 指定するには、パラメーターを受け取るオーバーロードを isolevel 使用します。

こちらもご覧ください

適用対象

BeginTransaction(IsolationLevel)

指定した IsolationLevel 値を使用して、データ ソースでトランザクションを開始します。

public:
 System::Data::Odbc::OdbcTransaction ^ BeginTransaction(System::Data::IsolationLevel isolevel);
public System.Data.Odbc.OdbcTransaction BeginTransaction (System.Data.IsolationLevel isolevel);
override this.BeginTransaction : System.Data.IsolationLevel -> System.Data.Odbc.OdbcTransaction
member this.BeginTransaction : System.Data.IsolationLevel -> System.Data.Odbc.OdbcTransaction
Public Function BeginTransaction (isolevel As IsolationLevel) As OdbcTransaction

パラメーター

isolevel
IsolationLevel

この接続のトランザクション分離レベル。 分離レベルを指定しない場合、ドライバーの既定の分離レベルが使用されます。

戻り値

OdbcTransaction

新しいトランザクションを表すオブジェクト。

例外

トランザクションは、現在アクティブです。 並列トランザクションはサポートされていません。

次の例では、OdbcConnection.OdbcTransaction また、メソッドの使用方法BeginTransactionCommitRollbackについても説明します。

public static void ExecuteTransaction(string connectionString)
{
    using (OdbcConnection connection =
               new OdbcConnection(connectionString))
    {
        OdbcCommand command = new OdbcCommand();
        OdbcTransaction transaction = null;

        // Set the Connection to the new OdbcConnection.
        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 OdbcConnection(connectionString)
        Dim command As New OdbcCommand()
        Dim transaction As OdbcTransaction

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

注釈

トランザクションをコミットまたはロールバックするには、or メソッドを明示的に使用するCommitRollback必要があります。

ODBC トランザクション管理モデルの.NET Framework Data Providerが正しく実行されるようにするには、データ ソースによって提供されるトランザクション管理モデルなど、他のトランザクション管理モデルを使用しないようにします。

こちらもご覧ください

適用対象