次の方法で共有


SqlConnection.BeginTransaction メソッド

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

オーバーロードの一覧

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function BeginTransaction() As SqlTransaction

[C#] public SqlTransaction BeginTransaction();

[C++] public: SqlTransaction* BeginTransaction();

[JScript] public function BeginTransaction() : SqlTransaction;

分離レベルを指定して、データベース トランザクションを開始します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function BeginTransaction(IsolationLevel) As SqlTransaction

[C#] public SqlTransaction BeginTransaction(IsolationLevel);

[C++] public: SqlTransaction* BeginTransaction(IsolationLevel);

[JScript] public function BeginTransaction(IsolationLevel) : SqlTransaction;

トランザクション名を指定して、データベース トランザクションを開始します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function BeginTransaction(String) As SqlTransaction

[C#] public SqlTransaction BeginTransaction(string);

[C++] public: SqlTransaction* BeginTransaction(String*);

[JScript] public function BeginTransaction(String) : SqlTransaction;

分離レベルとトランザクション名を指定して、データベース トランザクションを開始します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Function BeginTransaction(IsolationLevel, String) As SqlTransaction

[C#] public SqlTransaction BeginTransaction(IsolationLevel, string);

[C++] public: SqlTransaction* BeginTransaction(IsolationLevel, String*);

[JScript] public function BeginTransaction(IsolationLevel, String) : SqlTransaction;

使用例

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

[Visual Basic, C#, C++] メモ   ここでは、BeginTransaction のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
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(IsolationLevel.ReadCommitted, "SampleTransaction")
    ' 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("SampleTransaction")
      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(IsolationLevel.ReadCommitted,"SampleTransaction");
    // 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("SampleTransaction");
      }
      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(IsolationLevel::ReadCommitted,S"SampleTransaction");
    // 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(S"SampleTransaction");
      }
      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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

SqlConnection クラス | SqlConnection メンバ | System.Data.SqlClient 名前空間