次の方法で共有


OleDbConnection.BeginTransaction メソッド ()

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

Overloads Public Function BeginTransaction() As OleDbTransaction
[C#]
public OleDbTransaction BeginTransaction();
[C++]
public: OleDbTransaction* BeginTransaction();
[JScript]
public function BeginTransaction() : OleDbTransaction;

戻り値

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

例外

例外の種類 条件
InvalidOperationException 並列トランザクションはサポートされていません。

解説

トランザクションは、 Commit メソッドまたは Rollback メソッドを使用して、明示的にコミットまたはロールバックする必要があります。.NET Framework Data Provider for OLE DB のトランザクション管理モデルを正しく実行するために、データ ソースが提供するモデルなどの他のトランザクション管理モデルは使用しないでください。

メモ   分離レベルを指定しない場合、基になるプロバイダの既定の分離レベルが使用されます。 BeginTransaction メソッドを使用して分離レベルを指定するには、 isolationLevel パラメータを受け取るオーバーロードを使用します。

使用例

[Visual Basic, C#, C++] OleDbConnectionOleDbTransaction を作成する例を次に示します。この例では、 BeginTransactionCommitRollback の各メソッドの使い方も示します。

 
Public Sub RunOleDbTransaction(myConnString As String)
    Dim myConnection As New OleDbConnection(myConnString)
    myConnection.Open()
      
    Dim myCommand As OleDbCommand = myConnection.CreateCommand()
    Dim myTrans As OleDbTransaction
       
    ' Start a local transaction
    myTrans = myConnection.BeginTransaction()
    ' Assign transaction 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 OleDbException
        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

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

   OleDbCommand myCommand = myConnection.CreateCommand();
   OleDbTransaction myTrans;

   // Start a local transaction
   myTrans = myConnection.BeginTransaction();
   // Assign transaction 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 (OleDbException 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 RunOleDbTransaction(String* myConnString)
 {
    OleDbConnection* myConnection = new OleDbConnection(myConnString);
    myConnection->Open();

    OleDbCommand* myCommand = myConnection->CreateCommand();
    OleDbTransaction* myTrans;

    // Start a local transaction
    myTrans = myConnection->BeginTransaction();
    // Assign transaction 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 (OleDbException* 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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

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

参照

OleDbConnection クラス | OleDbConnection メンバ | System.Data.OleDb 名前空間 | OleDbConnection.BeginTransaction オーバーロードの一覧