我正在开发一个C#项目,我目前遇到了MySQL数据读取器的问题。
以下是我正在使用的代码
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);
}
问题是当我单步执行代码时,它会循环执行数据读取器上的读取,总行数设置为13,因此数据读取器中有13行。我成功完成了循环中的所有内容,但由于某种原因,它会退出循环并且不会通过其余的行。
感谢您提供的任何帮助。
答案 0 :(得分:0)
从查询的外观来看,您的问题出在WHERE
子句中。
reports.id = 1 AND reports.id = bug_updates.BugID
我很确定你想摆脱reports.id = 1
,因为你已经过滤了BugID
。
答案 1 :(得分: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的构造函数中是否没有发生剩余的迭代
答案 2 :(得分:0)
如果你的代码正在中止循环,那么循环体可能会产生异常,因为在Visual Studio上生成异常的正文行上获得一个断点,按Ctrl + Alt + E并从对话框检查CLR异常,然后调试以重现错误。
现在,如果异常被超时抛出(服务器关闭了连接等),是因为你在读取调用之间花了太多时间在循环体中,为了避免它,在Reader之前发出这些查询:
set net_write_timeout = 999999;
set net_read_timeout = 999999;
将导致MySql Server对客户端代码“更耐心”。