2014-02-25 93 views
0

我正在C#項目上工作,我目前遇到了MySQL數據讀取器的問題。MySQLDataReader只有當有更多的行時循環一次

下面是我用

try 
{ 
    using (ConnectMySQLDB db = new ConnectMySQLDB(Configuration.databaseSettings)) 
    { 
     string query = "SELECT COUNT(*) AS `TotalRows`, reports.id AS `BugID`, DateReported, Software, Platform, Version, FirstName, " 
      + "LastName, Email, Summary, BugDescription, PinCode, SendUpdates, Priority, Summary " 
      + "FirstName, LastName, Email, Summary, " 
      + "bug_updates.id AS `UpdateID`, UpdateMessage FROM reports, software, platforms, versions, bug_updates " 
      + "WHERE reports.SoftwareID = software.id AND reports.PlatformID = platforms.id " 
      + "AND reports.VersionID = versions.id AND reports.id = 1 AND reports.id = bug_updates.BugID AND SendUpdates=1 " 
      + "AND BugUpdateNotificationSent='0'"; 
     using (MySqlCommand cmd = new MySqlCommand(query, db.conn)) 
     { 
      using (MySqlDataReader reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        totalEmails = reader.GetInt32("TotalRows"); 
        library.logging(methodInfo, string.Format("Found {0} bugs requiring update notification", totalEmails)); 
        if (totalEmails > 0) 
        { 
         currentEmailCount++; 
         EmailNotifications emailNotifications = new EmailNotifications(reader); 
         emailNotifications.sendBugReportUpdateEmailNotification(currentEmailCount, totalEmails); 
        } 
        else 
        { 
         library.logging(methodInfo, "No emails requiring to be sent for update notification"); 
        } 
       } 
      } 
     } 
    } 
} 
catch (MySqlException ex) 
{ 
    string error = string.Format("Failed to check if updates need to be sent. MySQL Error: {0}", ex.Message); 
    library.logging(methodInfo, error); 
    library.setAlarm(error, CommonTasks.AlarmStatus.Medium, methodInfo); 
} 
catch (Exception ex) 
{ 
    string error = string.Format("Failed to check if updates need to be sent. General Error: {0}", ex.Message); 
    library.logging(methodInfo, error); 
    library.setAlarm(error, CommonTasks.AlarmStatus.Medium, methodInfo); 
} 

問題是,當我通過代碼一個上午,就enetrs循環對數據讀取器進行讀取的代碼,總行獲得設置爲13 ,所以數據讀取器中有13行。我成功地完成了循環中的所有內容,但是由於某種原因,它退出了循環,並且沒有經過其餘的行。

感謝您提供的任何幫助。

+2

選擇計數(*)將只返回一行,我不知道如何運行。在選擇列表中使用聚合函數時,是否需要Group By子句? MySql不需要這個嗎? –

+0

你確定你有'AND reports.id = 1'的13條記錄嗎? – Steve

+0

經過我的調查,看起來MySql允許您在沒有Group By子句的情況下在Select列表中具有其他列。 –

回答

0

從查詢的外觀來看,您的問題出現在您的WHERE條款中。

reports.id = 1 AND reports.id = bug_updates.BugID 

我敢肯定你想擺脫reports.id = 1的,因爲你已經在你的BugID過濾。

+0

我一直在調查這一點。它在MySql中是允許的,但這樣做沒有意義。這裏有一篇文章提到了這個問題:http://rpbouman.blogspot.com/2007/05/debunking-group-by-myths.html –

+0

感謝鏈接@ChrisDunaway。我刪除了這個評論。 – TyCobb

0

雖然MySQL支持沒有GROUP BY的COUNT,但結果在使用時似乎是等同的。請參見下面的示例:

mysql> set session sql_mode = ''; 
Query OK, 0 rows affected (0.00 sec) 

mysql> create table tg(c1 int, c2 int); 
Query OK, 0 rows affected (0.39 sec) 

mysql> insert into tg values(1,1), (1,2), (2,1); 
Query OK, 3 rows affected (0.06 sec) 
Records: 3 Duplicates: 0 Warnings: 0 

mysql> select c1, count(*) from tg; 
+------+----------+ 
| c1 | count(*) | 
+------+----------+ 
| 1 |  3 | 
+------+----------+ 
1 row in set (0.02 sec) 

另一個建議是檢查如果剩餘迭代內部沒有EmailNotifications的構造

0

出現如果您的代碼中止循環,可能有人體產生異常爲了在Visual Studio中產生異常的正文的正確行處獲得斷點,請按Ctrl + Alt + E並從對話框中檢查CLR異常,然後調試以重現錯誤。

現在,如果超時拋出異常(服務器關閉連接等),是因爲您在讀取調用之間的循環體中花費了太多時間,爲了避免它,在讀取器之前發出這些查詢:

set net_write_timeout = 999999; 
set net_read_timeout = 999999; 

這將導致MySql服務器與客戶端代碼「更耐心」。