2012-04-13 4 views
1

私はそれをクリアしたいいくつかの混乱がありました。私はADO.NETを使用してデータベースに値を挿入しています。 5つ目のアイテムのデータを挿入しているときにエラーが発生した場合は、データベースに挿入したものをロールバックする必要がある場合、10個のアイテムを挿入したいとします。C#.NETのロールバックINSERTコマンド

私はちょうどトランザクションとロールバックの方法の概念を読んで、それもプログラムでそれを実装しようとしましたが、まだそれは4つの項目を挿入し、私に5番目の項目のエラーメッセージを与えます。挿入クエリをロールバックしません。

トランザクションとロールバックの方法で問題が解決したのですか、他の方法を使用する必要があります。ここ

for (int i = 0; i < itemLength - 1; i++) 
      { 
       //--- Start local transaction --- 
       myTrans = Class1.conn.BeginTransaction(); 

       //--- Assign transaction object and connection to command object for a pending local transaction --- 
       _insertQry = Class1.conn.CreateCommand(); 
       _insertQry.Connection = Class1.conn; 
       _insertQry.Transaction = myTrans; 

       _insertQry.CommandText = "INSERT INTO Product_PropertyValue(ItemNo, PropertyNo, ValueNo) VALUES (@ItemNo, @PropertyNo, @ValueNo)"; 

       //_insertQry = new SqlCommand("INSERT INTO Product_PropertyValue(ItemNo, PropertyNo, ValueNo) VALUES (@ItemNo, @PropertyNo, @ValueNo)", Class1.conn); 

       _insertQry.Parameters.AddWithValue("@ItemNo", _itemNo[i]); 
       _insertQry.Parameters.AddWithValue("@PropertyNo", _propNo); 
       _insertQry.Parameters.AddWithValue("@ValueNo", _propValue); 

       _insertQry.ExecuteNonQuery(); 
       myTrans.Commit(); 
      } 

誰も私を助けることができる、私のコードですか?

+0

"エラー"が例外の場合、トランザクションをtryブロック内に持ち、catch部分をロールバックして(そして最終的にはディップシング)(または使用をキャッチしようとしますか?) –

+0

ここは私のコードですinseertQry.ExecuteNonQuery(); myTransaction.Commit(); tryブロックとcatchのTransaction.Rollback(); –

+0

修正したコードのバージョンを見てください。 –

答えて

1

によって提供されるテンプレートに従うことをお勧めします私は)

1あなたのコードに2変更をしたので、ためにループの外でBeginTransaction()を移動しますすべての10個のINSERT文は、単一のトランザクションであることをあなたがそれらを使用すると、エラーが発生した場合にロールバックすることができるように原子

2)は、try/catchブロックを追加したい場合は、それはあなたが望むものです。

 //--- Start local transaction --- 
     myTrans = Class1.conn.BeginTransaction(); 
     bool success = true; 

     try 
     { 
      for (int i = 0; i < itemLength - 1; i++) 
      { 
       //--- Assign transaction object and connection to command object for a pending local transaction --- 
       _insertQry = Class1.conn.CreateCommand(); 
       _insertQry.Connection = Class1.conn; 
       _insertQry.Transaction = myTrans; 

       _insertQry.CommandText = "INSERT INTO Product_PropertyValue(ItemNo, PropertyNo, ValueNo) VALUES (@ItemNo, @PropertyNo, @ValueNo)"; 

       //_insertQry = new SqlCommand("INSERT INTO Product_PropertyValue(ItemNo, PropertyNo, ValueNo) VALUES (@ItemNo, @PropertyNo, @ValueNo)", Class1.conn); 

       _insertQry.Parameters.AddWithValue("@ItemNo", _itemNo[i]); 
       _insertQry.Parameters.AddWithValue("@PropertyNo", _propNo); 
       _insertQry.Parameters.AddWithValue("@ValueNo", _propValue); 

       _insertQry.ExecuteNonQuery(); 
      } 
     } 
     catch (Exception ex) 
     { 
      success = false; 
      myTrans.Rollback(); 
     } 

     if (success) 
     { 
      myTrans.Commit(); 
     } 

これが機能しない場合はお知らせください。

+0

myTrans.Commit()はエラーメッセージを表示しているので、try-catchブロックの外側にある必要があります。私はそれを最後のブロックに入れようとしましたが、それでも同じエラーです。 –

+0

どのようなエラーがありましたか? –

+0

SqlTransactionが使用されていません。未処理のSqlExceptionエラーが発生しました。 –

2

アトミックコミットを達成しようとしているようです。完全に挿入するか、まったく挿入しません。

SqlTransaction objTrans = null; 
     using (SqlConnection objConn = new SqlConnection(strConnString)) 
     { 
      objConn.Open(); 
      objTrans = objConn.BeginTransaction(); 
      SqlCommand objCmd1 = new SqlCommand("insert into tbExample values(1)", objConn); 
      SqlCommand objCmd2 = new SqlCommand("insert into tbExample values(2)", objConn); 
      try 
      { 
       objCmd1.ExecuteNonQuery(); 
       objCmd2.ExecuteNonQuery(); 
       objTrans.Commit(); 
      } 
      catch (Exception) 
      { 
       objTrans.Rollback(); 
      } 
      finally 
      { 
       objConn.Close(); 
      } 

のようなものを試してみてくださいまた、右のパス上にある http://www.codeproject.com/Articles/10223/Using-Transactions-in-ADO-NET

0

を見て、あなたがエラー時にロールバックすることができるようになりますので、ADO.NETは、トランザクションをサポートしています。

ここにコードを掲載すると、より具体的なガイダンスが得られます。あなたの質問は非常に一般的であるため、しかし、私は、MSDN

using (SqlConnection connection = new SqlConnection(connectionString)) 
{ 
    connection.Open(); 

    // Start a local transaction. 
    SqlTransaction sqlTran = connection.BeginTransaction(); 

    // Enlist a command in the current transaction. 
    SqlCommand command = connection.CreateCommand(); 
    command.Transaction = sqlTran; 

    try 
    { 
     // Execute two separate commands. 
     command.CommandText = 
      "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong size')"; 
     command.ExecuteNonQuery(); 
     command.CommandText = 
      "INSERT INTO Production.ScrapReason(Name) VALUES('Wrong color')"; 
     command.ExecuteNonQuery(); 

     // Commit the transaction. 
     sqlTran.Commit(); 
     Console.WriteLine("Both records were written to database."); 
    } 
    catch (Exception ex) 
    { 
     // Handle the exception if the transaction fails to commit. 
     Console.WriteLine(ex.Message); 

     try 
     { 
      // Attempt to roll back the transaction. 
      sqlTran.Rollback(); 
     } 
     catch (Exception exRollback) 
     { 
      // Throws an InvalidOperationException if the connection 
      // is closed or the transaction has already been rolled 
      // back on the server. 
      Console.WriteLine(exRollback.Message); 
     } 
    } 
} 
+0

私の質問に自分のコードを投稿させて、間違いを犯しているのを理解できるかもしれません。 –

関連する問題