2011-12-14 59 views
0

我正在循環這些mysql行和處理數據。但是,在部分處理過程中,我也想更新到同一個mysql表中。麻煩更新到mysql裏面的閱讀器循環

這不適合我。

command.CommandText = "UPDATE outbox SET `faxpro` = 'DONE' WHERE `id` = '" + id + "'"; 
       MySqlDataReader result = command.ExecuteReader(); 

CODE

string connString = "Server=localhost;Port=3306;Database=communications;Uid=myuser;password=mypass;"; 
      MySqlConnection conn = new MySqlConnection(connString); 
      MySqlCommand command = conn.CreateCommand(); 
      command.CommandText = "SELECT * FROM outbox WHERE `faxstat` = 'Y' AND `fax` <> '' AND `faxpro` = 'PENDING'"; 


     try 
     { 

      conn.Open(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
     MySqlDataReader reader = command.ExecuteReader(); 

     if (reader.HasRows) 
     { 

      while (reader.Read()) 
      { 
       Console.WriteLine(reader["account"].ToString()); 
       SendFax(reader["filepath"].ToString(), reader["filepath"].ToString(), reader["id"].ToString(), reader["fax"].ToString()); 
       string id = reader["id"].ToString(); 
       command.CommandText = "UPDATE outbox SET `faxpro` = 'DONE' WHERE `id` = '" + id + "'"; 
       MySqlDataReader result = command.ExecuteReader(); 

      } 
     } 
+1

*「這不適合我」*是什麼意思?順便說一下,你不僅應該將`conn.Open();`嵌套在`Try-Catch`中,而且最重要的是`command.ExecuteReader();`。 – 2011-12-14 16:57:05

回答

1

我認爲最後Command.ExecuteReader卻()試圖打開一個更易於閱讀,但它是不可能的,一個與做。先關閉首先打開的閱讀器,然後修改此表;或嘗試使用command.ExecuteNonQuery()方法。

+0

謝謝。我添加了第二個連接並用於更新。 – 2011-12-14 19:43:07