2014-07-01 56 views
-1

這是MySQL表如何通過c#代碼更新mysql表?

reminder_id  date  show_day  before_day status 
    1   2016-06-05 1999-01-01   2  p 
    2   2016-06-05 1999-01-01   1  p 
    3   2016-06-14 1999-01-01   2  p 
    4   2016-06-25 1999-01-01   0  p 
    5   2016-06-26 1999-01-01   4  p 

我要更新的show_day列作爲(日期before_day)在ID 1 的show_day將( '2016-06-05'-2)=' 2016-06-03' in id 2 it will('2016-06-05'-1)='2016-06-01'等等......

我寫下面的代碼,但它將整列更新爲第一個id的相同值。 plz幫助我。


 void init2() 
     { 
     DateTime date = default(DateTime); 
     int before_day = 0; 
     DateTime sToday = DateTime.Now; 
     string myConnection = "datasource= localhost;port=3306;username=root;password=root"; 
     MySqlConnection myConn = new MySqlConnection(myConnection); 
     MySqlCommand SelectCommand = new MySqlCommand("SELECT* FROM bs.reminder ", myConn); 
     MySqlDataReader myReader; 
     myConn.Open(); 
     myReader = SelectCommand.ExecuteReader(); 
     while (myReader.Read()) 
     { 
      date = myReader.GetDateTime(1); 
      before_day = myReader.GetInt32(4); 
      string show_day = date.AddDays(-before_day).ToString("yyyy/MM/dd"); 
      string constring = "datasource= localhost;port=3306;username=root;password=root"; 
      string Query = "update bs.reminder set status='s',show_day='" + show_day + "' where status= 'p';"; 
      MySqlConnection conDataBase = new MySqlConnection(constring); 
      MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase); 
      MySqlDataReader myReader1; 
      try 
      { 
       conDataBase.Open(); 
       myReader1 = cmdDataBase.ExecuteReader(); 
        while (myReader1.Read()) 
       { 

       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 


     } 
+0

究竟是什麼問題? –

+0

你爲什麼在代碼開始時打開MySQL連接,然後在循環中反覆打開一個?只需要第一個,我認爲 – vogomatix

+0

它將整列更新爲第一個id的相同值 – user3399062

回答

0

有幾件事情你的代碼錯誤,但沒有更新發生的原因是你缺少

cmdDataBase.ExecuteNonQuery(); 

[我要指出,你的變量命名可以更好地幫助理解。]

1

我會使用ExecuteNonQuery而不是ExecuteReader。
而參數化查詢總是比字符串連接更好。

但是,更新失敗的原因是由於WHERE子句中的主鍵缺少信息。您需要將reminder_id值添加到哪裏來的更新僅限制於特定記錄在當前外部讀者環

int before_day = 0; 
DateTime sToday = DateTime.Now; 
string myConnection = "datasource= localhost;port=3306;username=root;password=root"; 
using(MySqlConnection myConn = new MySqlConnection(myConnection)) 
using(MySqlCommand SelectCommand = new MySqlCommand("SELECT* FROM bs.reminder ", myConn)) 
{ 
    myConn.Open(); 
    using(MySqlDataReader myReader = SelectCommand.ExecuteReader()) 
    { 
     while (myReader.Read()) 
     { 
      // get the remainder_id. It is the primary key to use in the update where 
      int reminderid = myReader.GetInt32(0); 
      date = myReader.GetDateTime(1); 
      before_day = myReader.GetInt32(4); 

      DateTime show_day = date.AddDays(-before_day); 
      string constring = "datasource= localhost;port=3306;username=root;password=root"; 

      // Update query that limits to just the current record (reminder_id) 
      string Query = @"update bs.reminder set status='s',[email protected] 
          where [email protected] and status= 'p';"; 

      using(MySqlConnection conDataBase = new MySqlConnection(constring)) 
      using(MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase)) 
      { 
       cmdDataBase.Parameters.AddWithValue("@id", reminderid); 
       cmdDataBase.Parameters.AddWithValue("@newDay", show_day); 
       try 
       { 
        conDataBase.Open(); 
        int rowAffected = cmdDataBase.ExecuteNonQuery(); 
        .... 
       } 
      } 
     } 
    } 
} 

另一種優化可能是using語句來確保關閉和處置的內在聯繫和命令

+0

它將整列(全部ID)更新爲第一個ID的相同值 – user3399062

+0

用reminder_id上的條件更新了答案。刷新 – Steve

+0

史蒂夫..它的作品。謝謝.. – user3399062