2014-02-25 1 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); 
} 
I 코드를 단계별 때 문제가

사용시, 그것은 데이터 판독기의 판독을 수행하는 루프 enetrs 코드이며, 전체 행은 13로 설정 얻을 따라서 데이터 리더에는 13 개의 행이 있습니다. 루프 내에서 모든 것을 성공적으로 완료했지만 어떤 이유로 루프에서 빠져 나와 나머지 행은 통과하지 않습니다.

도움을 주셔서 감사합니다.

+2

선택 개수 (*)는 한 행만 반환하며 어떻게 실행되는지는 잘 모르겠습니다. 선택 목록에서 집계 함수를 사용할 때 Group By 절이 필요하지 않습니까? MySQL은 이것을 요구하지 않습니까? –

+0

'AND reports.id = 1'이 (가) 13 개의 레코드가 있습니까? – Steve

+0

약간의 조사가 끝나면 MySql에서 Group By 절없이 선택 목록에 추가 열을 포함 할 수 있습니다. –

답변

0

검색어의 모양에 따라 귀하의 문제는 WHERE 조항에 있습니다.

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

난 당신이 이미 BugID 필터링되기 때문에 reports.id = 1 제거 할 확신합니다.

+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 예외를 확인한 다음 디버그하여 오류를 재현하십시오.

이제 예외가 발생하여 제한 시간 (서버가 연결을 종료 한 경우)이 발생하면 읽기 호출 사이에 루프 본문에 너무 많은 시간을 소비하여이를 피하기 위해 Reader :

set net_write_timeout = 999999; 
set net_read_timeout = 999999; 

이렇게하면 MySql 서버가 클라이언트 코드로 "환자"가됩니다.

관련 문제