次の方法で共有


IDbTransaction インターフェイス

データ ソースで実行するトランザクションを示します。リレーショナル データベースにアクセスする .NET Framework データ プロバイダによって実装されます。

この型のすべてのメンバの一覧については、IDbTransaction メンバ を参照してください。

System.IDisposable
   System.Data.IDbTransaction

Public Interface IDbTransaction
   Inherits IDisposable
[C#]
public interface IDbTransaction : IDisposable
[C++]
public __gc __interface IDbTransaction : public IDisposable
[JScript]
public interface IDbTransaction implements IDisposable

IDbTransaction を実装するクラス

クラス 説明
OdbcTransaction データ ソースで作成する SQL トランザクションを表します。このクラスは継承できません。
OleDbTransaction データ ソースで作成する SQL トランザクションを表します。このクラスは継承できません。
OracleTransaction データベースで実行するトランザクションを表します。このクラスは継承できません。
SqlCeTransaction データ ソースで作成する SQL トランザクションを表します。このクラスは継承できません。
SqlTransaction SQL Server データベースで作成する Transact-SQL トランザクションを表します。このクラスは継承できません。

解説

IDbTransaction インターフェイスによって、継承クラスはデータ ソースで実行するトランザクションを示す Transaction クラスを実装できます。Transaction クラスの詳細については、「 トランザクションの実行 」を参照してください。.NET Framework データ プロバイダの実装の詳細については、「 .NET Framework データ プロバイダの実装 」を参照してください。

アプリケーションでは IDbTransaction インターフェイスのインスタンスは直接作成されず、 IDbTransaction を継承するクラスのインスタンスが作成されます。

IDbTransaction を継承するクラスは、継承されたメンバを実装する必要があり、通常、プロバイダ固有の機能を追加する追加メンバを定義する必要があります。たとえば、 IDbTransaction インターフェイスが Commit メソッドを定義します。次に、 OleDbTransaction クラスがこのプロパティを継承し、 Begin メソッドを定義します。

実装時の注意: .NET Framework データ プロバイダ間に一貫性を持たせるために、継承クラスには Prv Transaction という形式の名前を付けてください。 Prv は、各 .NET Framework データ プロバイダの名前空間内のすべてのクラスに付けられるプリフィックスです。たとえば、 SqlSystem.Data.SqlClient 名前空間内の SqlTransaction クラスのプリフィックスです。

使用例

[Visual Basic, C#, C++] 派生クラス SqlConnection および SqlTransaction のインスタンスを作成する例を次に示します。 BeginTransactionCommitRollback の各メソッドの使い方も示します。

 
Public Sub RunSqlTransaction(myConnString As String)
    Dim myConnection As New SqlConnection(myConnString)
    myConnection.Open()
    
    Dim myCommand As SqlCommand = myConnection.CreateCommand()
    Dim myTrans As SqlTransaction
    
    ' Start a local transaction
    myTrans = myConnection.BeginTransaction()
    ' Must assign both transaction object and connection
    ' to Command object for a pending local transaction
    myCommand.Connection = myConnection
    myCommand.Transaction = myTrans
    
    Try
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
      myCommand.ExecuteNonQuery()
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
      myCommand.ExecuteNonQuery()
      myTrans.Commit()
      Console.WriteLine("Both records are written to database.")
    Catch e As Exception
      Try
        myTrans.Rollback()
      Catch ex As SqlException
        If Not myTrans.Connection Is Nothing Then
          Console.WriteLine("An exception of type " & ex.GetType().ToString() & _
                            " was encountered while attempting to roll back the transaction.")
        End If
      End Try
    
      Console.WriteLine("An exception of type " & e.GetType().ToString() & _
                      "was encountered while inserting the data.")
      Console.WriteLine("Neither record was written to database.")
    Finally
      myConnection.Close()
    End Try
End Sub 'RunSqlTransaction

[C#] 
public void RunSqlTransaction(string myConnString) 
 {
    SqlConnection myConnection = new SqlConnection(myConnString);
    myConnection.Open();

    SqlCommand myCommand = myConnection.CreateCommand();
    SqlTransaction myTrans;

    // Start a local transaction
    myTrans = myConnection.BeginTransaction();
    // Must assign both transaction object and connection
    // to Command object for a pending local transaction
    myCommand.Connection = myConnection;
    myCommand.Transaction = myTrans;

    try
    {
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
      myCommand.ExecuteNonQuery();
      myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
      myCommand.ExecuteNonQuery();
      myTrans.Commit();
      Console.WriteLine("Both records are written to database.");
    }
    catch(Exception e)
    {
      try
      {
        myTrans.Rollback();
      }
      catch (SqlException ex)
      {
        if (myTrans.Connection != null)
        {
          Console.WriteLine("An exception of type " + ex.GetType() +
                            " was encountered while attempting to roll back the transaction.");
        }
      }
    
      Console.WriteLine("An exception of type " + e.GetType() +
                        " was encountered while inserting the data.");
      Console.WriteLine("Neither record was written to database.");
    }
    finally 
    {
      myConnection.Close();
    }
}

[C++] 
public:
void RunSqlTransaction(String* myConnString) 
 {
    SqlConnection* myConnection = new SqlConnection(myConnString);
    myConnection->Open();

    SqlCommand* myCommand = myConnection->CreateCommand();
    SqlTransaction* myTrans;

    // Start a local transaction
    myTrans = myConnection->BeginTransaction();
    // Must assign both transaction object and connection
    // to Command object for a pending local transaction
    myCommand->Connection = myConnection;
    myCommand->Transaction = myTrans;

    try
    {
      myCommand->CommandText = S"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
      myCommand->ExecuteNonQuery();
      myCommand->CommandText = S"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
      myCommand->ExecuteNonQuery();
      myTrans->Commit();
      Console::WriteLine(S"Both records are written to database.");
    }
    catch(Exception* e)
    {
      try
      {
        myTrans->Rollback();
      }
      catch (SqlException* ex)
      {
        if (myTrans->Connection != 0)
        {
          Console::WriteLine(S"An exception of type {0} was encountered while attempting to roll back the transaction.", ex->GetType());
        }
      }
    
      Console::WriteLine(S"An exception of type {0} was encountered while inserting the data.", e->GetType());
      Console::WriteLine(S"Neither record was written to database.");
    }
    __finally 
    {
      myConnection->Close();
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Data

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System.Data (System.Data.dll 内)

参照

IDbTransaction メンバ | System.Data 名前空間