2014-02-06 39 views
0

我有這樣的代碼在我的WinForm C#應用程序:的MessageBox後提交的C#

public void InsertValidationDate(Validar v, string descanso) 
     { 
      bool insert = false; 

      try 
      { 
       bd.con.Open(); 
       bdag.bd.trans = bdag.bd.con.BeginTransaction(); 

       insert = InsertDate(v); 

       if (insert == true) 
       { 
        MessageBox.Show("Insert Ok"); 

        pk = ReturnInsertDate(DateTime.Parse(v.Date), v.User); 
        insert = UpdateReference(DateTime.Parse(v.Date), pk); 

        if (insert == false) 
        { 
         Rollback(); 
         MessageBox.Show("Update Error");       
        } 
        else 
        { 
         Commit(); 
         MessageBox.Show("Update OK"); 
        } 
       }     

      } 
      catch (Exception ex) 
      { 
       Rollback(); 

      } 
      finally 
      { 
       con.Close(); 
      } 
     } 

顯示消息(「插入OK」)連接到數據庫關閉後,交易是不是更多鈔票COMMITED。爲什麼?因爲Insert函數不關閉數據庫連接。有任何想法嗎?謝謝!!。

 public int ReturnInsertDate(DateTime date, int user) 
     { 
      int validate = -1; 

      try 
      { 
       bd.CadSQL = "select " + bd.Validar.id + " from " + bd.Validar.tabla + " where "; 
       bd.CadSQL += bd.Validar.date + " = '" + date.Year + "-" + date.Month.ToString("d2") + "-" + date.Day.ToString("d2") + "'"; 
       bd.CadSQL += " and " + bd.Validar.user+ " = " + user; 

       bd.comandoSQL.CommandText = bd.CadSQL; 
       bd.reader = bd.comandoSQL.ExecuteReader(); 

       while (bd.reader.Read()) 
       { 
        if (!bd.reader.IsDBNull(0)) 
         validate = bd.reader.GetInt32(0); 
       } 
      } 
      catch (Exception e) 
      { 
       MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
      finally 
      { 
       bd.reader.Close(); 
      } 

      return validate; 
     } 

     public bool UpdateReference(DateTime date, int validate) 
     { 
      bool updateR = false; 

      try 
      { 
       bd.CadSQL = "update " + bd.Fichar.tabla; 
       bd.CadSQL += " set " + bd.Fichar.validateId + " = " + validate; 
       bd.CadSQL += " where " + bd.Fichar.date + " = '" + date.Year + "-" + date.Month.ToString("d2") + "-" + date.Day.ToString("d2") + "'"; 

       bd.comandoSQL.CommandText = bd.CadSQL; 
       bd.comandoSQL.ExecuteNonQuery(); 

       updateR = true; 
      } 
      catch (Exception e) 
      { 
       MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 

      return updateR ; 
     } 
+0

什麼是插入(V)..... – pankeel

+0

插入是插入到數據庫中,我在我的工作表中的驗證工作小時後,我更新這個ID驗證到其他表的功能。謝謝 – juanchoelx

+3

您是否關閉了InsertDate,ReturnInsertDate或UpdateReference中的連接? – Steve

回答

1

我會設置你的交易如下,因爲它似乎你正在處理不同的連接對象。

using (var scope = TransactionHelper.Instance) 
{ 
var process = true; 
var aMessage = ""; 
try 
{ 
    //Enter you code and functions here 
    //Whenever you find a piece of code to be working incorrectly, Raise an error 
    if(!someCode(blabla)) 
     throw new Exception("This someCode quited on me!"); 
    //Whenever your code does what it needs to do, end with a commit 
    scope.Commit(); 
} 
catch(Exception ex) //Catch whatever exception might occur 
{ 
    scope.FullRollBack(); 
    aMessage = ex.Message; 
    process = false; 
} 
finally 
{ 
    //only here you will use messageboxes to explain what did occur during the process 
    if (process) 
    MessageBox.Show("Succesfully Committed!"); 
    else 
    MessageBox.Show(string.Format("Rollback occurred, exception Message: {0}" 
        , aMessage); 
} 
} 
+0

什麼是TransactionHelper的使用參考? – juanchoelx

+0

在使用IDisposable對象(如連接)時,使用引用會有所幫助(您不希望連接始終處於打開狀態;事務幫助器僅僅是站在所有其他連接之上,並且在它的結尾將會提交或回滾所有操作那可能/應該發生了,我們使用這個場景進行測試 – Schuere

0

當你打開連接,然後使用bdag.bd.conBeginTransaction呼叫這是一個有點奇怪,你指的是con。你絕對肯定這兩個變量指向同一個連接對象嗎?

+0

大概你是在正確的軌道上。另外,Commit()和Rollback()作爲獨立的方法暗示某事正在處理不同的對象 – Steve

+0

是的,當我編寫代碼時,這是一個錯誤。抱歉。 – juanchoelx