我正在寻找一个星期的间隔,但6岁以下的任何东西都给了我:
timeIn > DATE_SUB(NOW(), INTERVAL 1 WEEK)
Error: Invalid attempt to Read when reader is closed.
timeIn > DATE_SUB(NOW(), INTERVAL 6 WEEK) //6 or anything above 6 and the query works
Error: Invalid attempt to Read when reader is closed.
以下是我正在执行查询的方式(这有效,但是当我将INTERVAL从6更改为更少的东西时,我的读取器被关闭错误):
string sql = "SELECT rooms.building, rooms.room, " +
"users.FirstName, users.LastName, users.adUname, " +
"ingressegresslogs.timeIn, ingressegresslogs.timeOut, rooms.Id " +
"FROM rooms, users, ingressegresslogs " +
"WHERE ingressegresslogs.RoomId = rooms.Id " +
"AND ingressegresslogs.timeIn > DATE_SUB(NOW(), INTERVAL 6 WEEK) "
"AND ingressegresslogs.UserId = users.id " +
"ORDER BY rooms.Id ASC, ingressegresslogs.timeIn ASC";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
编辑:此查询的工作原理是mysql命令行和INTERVAL 1 WEEK,而不是C#代码。返回带有数据的428行
这是读者代码,可能是问题所在。
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
if (!rdr.HasRows)
{
rdr.Close();
// EventLog.WriteEntry("IEReporter", "Database returned nothing", EventLogEntryType.Error, 234);
}
while (rdr.Read())
{
try
{
IELog queryResult = new IELog();
queryResult.RoomID = rdr.GetString("Id");
queryResult.FirstName = rdr.GetString("FirstName");
queryResult.LastName = rdr.GetString("LastName");
queryResult.ADUname = rdr.GetString("ADUname");
queryResult.Building = rdr.GetString("Building");
queryResult.Room = rdr.GetString("room");
queryResult.Ingresstime = rdr.GetDateTime ("timeIn");
// timeOut might be null
if (Convert.IsDBNull(rdr["timeOut"]))
{
queryResult.Egresstime = new DateTime(1111, 1, 11);
}
else
{
queryResult.Egresstime = rdr.GetDateTime("timeOut");
}
queryResultList.Add(queryResult);
}
catch (Exception ex)
{
Console.WriteLine("LogDAO build list: " + ex);
}
}
// Count rows
//Console.WriteLine("Row Count: {0}", queryResultList.Count);
rdr.Close();
return queryResultList;
答案 0 :(得分:1)
SQL适用于我:
mysql> select DATE_SUB(NOW(), INTERVAL 1 WEEK);
+----------------------------------+
| DATE_SUB(NOW(), INTERVAL 1 WEEK) |
+----------------------------------+
| 2011-07-07 01:16:50 |
+----------------------------------+
你的问题是你没有返回任何行,这就是读者爆炸的原因吗?
答案 1 :(得分:1)
查看您发布的代码,您可能希望在确定没有行后立即返回。
if (!rdr.HasRows)
{
rdr.Close();
// EventLog.WriteEntry("IEReporter", "Database returned nothing", EventLogEntryType.Error, 234);
return; // do this, or otherwise skip the read below
}