简体   繁体   中英

MySQLDataReader only looping once when there are more rows

I am working on a C# project and I am currently experiencing an issue with a MySQL Data Reader.

Below is the code that I a am using

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);
}

The problem is when I step through the code, it enetrs the loop to perform the read on the data reader, the total rows get set to 13, so there are 13 rows in the data reader. I successfully complete everything within the loop, but it then for some reason drops out of the loop and doesn't go through the rest of the rows.

Thanks for any help you can provide.

From the looks of your query, your issue is in your WHERE clause.

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

I am pretty sure you want to get rid of the reports.id = 1 since you are already filtering on your BugID .

Although MySQL support COUNT without GROUP BY, the result seems equivalent when used. See the sample below:

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)

Another suggestion would be to check if the remaining iterations are not occurring inside EmailNotifications's constructor

If your code is aborting the loop, may there's an exception generated by the body of the loop, for getting a breakpoint at the exact line of the body that generates the exception on Visual Studio press Ctrl+Alt+E and from the dialog check CLR exceptions, then debug to reproduce the error.

Now, if the exception is thrown by timeout (server closed the connection, etc), is because your are spending too much time in the loop body between Read calls, to avoid it, issue these queries before the Reader:

set net_write_timeout = 999999;
set net_read_timeout = 999999;

that will cause the MySql Server to be "more patient" with client code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM