2012-04-13 27 views
1

我有些困惑,我想清除它 - 我使用ADO.NET將值插入到數據庫中。假設我想插入10個項目,如果在插入第5個項目的數據時遇到錯誤,它應該回滾我插入數據庫的任何內容。C#.NET中的回滾INSERT命令

我剛剛讀了Transaction and Rollback方法的概念,並試圖在程序中實現它,但仍然插入了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

如果「error」是一個異常,你是否在try塊中有你的事務,並且在catch部分回滾(並在finally中減少)(或者嘗試使用catch)? – 2012-04-13 19:24:40

+0

這裏是我的代碼inseertQry.ExecuteNonQuery(); myTransaction.Commit();在try塊和catch中Transaction.Rollback(); – 2012-04-13 19:39:20

+0

看看你的代碼的修改版本。 – 2012-04-13 20:29:36

回答

1

提供的模板,我做了2修改代碼

1)移動的BeginTransaction()外的for循環,所以您的所有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塊外面,因爲它給了我錯誤信息。我試圖把它放在最後一塊,但仍然是一樣的錯誤,除此之外它確實有效。 – 2012-04-13 20:28:58

+0

你得到了什麼錯誤? – 2012-04-13 20:44:18

+0

SqlTransaction未使用。未處理的SqlException發生錯誤。 – 2012-04-13 20:53:50

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

讓我在我的問題中發佈我的代碼,以便您可以瞭解我在哪個點出錯。 – 2012-04-13 19:46:45