C# 当有更多行时,MySQLDataReader仅循环一次
我正在从事一个C#项目,目前遇到MySQL数据读取器的问题 下面是我正在使用的代码C# 当有更多行时,MySQLDataReader仅循环一次,c#,mysql,C#,Mysql,我正在从事一个C#项目,目前遇到MySQL数据读取器的问题 下面是我正在使用的代码 try { using (ConnectMySQLDB db = new ConnectMySQLDB(Configuration.databaseSettings)) { string query = "SELECT COUNT(*) AS `TotalRows`, reports.id AS `BugID`, DateReported, Software, Platform,
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行。我成功地完成了循环中的所有内容,但由于某种原因,它会退出循环,不会遍历其余的行
谢谢你能提供的帮助 从查询的外观来看,您的问题在
WHERE
子句中
reports.id = 1 AND reports.id = bug_updates.BugID
我很确定你想摆脱
reports.id=1
,因为你已经在你的BugID
上进行过滤了。虽然MySQL支持计数没有分组依据,但使用时结果似乎是一样的。请参见下面的示例:
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的构造函数中是否没有发生剩余的迭代如果您的代码正在中止循环,则循环体可能会生成异常,要在VisualStudio上生成异常的正文的精确行上获取断点,请按Ctrl+Alt+E,然后从对话框中检查CLR exceptions,然后调试以再现错误 现在,如果异常是由超时引发的(服务器关闭了连接等),是因为您在读取调用之间的循环体中花费了太多时间,为了避免这种情况,请在读取器之前发出以下查询:
set net_write_timeout = 999999;
set net_read_timeout = 999999;
这将导致MySql服务器对客户端代码“更有耐心”。Select Count(*)将只返回一行,我不确定该如何运行。在Select列表中使用聚合函数时,不需要GROUPBY子句吗?MySql不需要这样做吗?您确定您有13条记录,其中包含
和reports.id=1
?在我这方面做了一些调查之后,MySql似乎允许您在Select列表中增加列,而不需要Group By子句。我一直在调查这一点。MySql中允许这样做,但这样做没有意义。这里有一篇文章提到了这个问题:感谢@ChrisDunaway的链接。我删除了这句话。