我正在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行。我成功地完成了循環中的所有內容,但是由於某種原因,它退出了循環,並且沒有經過其餘的行。
感謝您提供的任何幫助。
選擇計數(*)將只返回一行,我不知道如何運行。在選擇列表中使用聚合函數時,是否需要Group By子句? MySql不需要這個嗎? –
你確定你有'AND reports.id = 1'的13條記錄嗎? – Steve
經過我的調查,看起來MySql允許您在沒有Group By子句的情況下在Select列表中具有其他列。 –