oracle Reader.Read() 即使有行也无法读取行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13843267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reader.Read() fails to read rows even though it has rows
提问by SiliVestige
I have a problem where it appears that the reader indicates that it has rows from the returned SQL but the while loop for the reader never runs. I put a messagebox in the reader.hasrows as a verification and I put a messagebox on the first line after the while loop as well. The messagebox for the hasrows is executed but the messagebox for the Read is not executed. It is very puzzling. I tried the query against the database and it indeed does return rows. Here is the code snippet.
我有一个问题,似乎读取器表明它有来自返回的 SQL 的行,但读取器的 while 循环从未运行。我在 reader.hasrows 中放置了一个消息框作为验证,并且在 while 循环之后的第一行也放置了一个消息框。已执行 hasrows 的消息框,但未执行 Read 的消息框。这是非常令人费解的。我尝试了对数据库的查询,它确实返回了行。这是代码片段。
using (DbConnection connection = CADBase.DbProviderFactory.CreateConnection())
{
connection.ConnectionString = CADBase.DbConnectionString;
connection.Open();
using (DbCommand command = connection.CreateCommand())
{
SQL = <statement here>;
command.CommandText = SQL
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
//NEVER MAKES IT HERE
}
}
}
}
回答by Robert Harvey
To future readers of this question: note that the problem occurred because the OP was returning too many columns in the query. See the comments below this answer.
对于此问题的未来读者:请注意,问题的发生是因为 OP 在查询中返回了太多列。请参阅此答案下方的评论。
I'm not quite sure why this is happening, but you really only need to check for rows once, not twice, and the Read()
method already does this.
我不太确定为什么会发生这种情况,但您确实只需要检查行一次,而不是两次,并且该Read()
方法已经这样做了。
So all you really need is
所以你真正需要的是
while (reader.Read())
{
// Do your thing
}
回答by Hylaean
call MoveNext() before first call to read
在第一次调用读取之前调用 MoveNext()
回答by Sergiu
You should use reader.HasRows property and not the method.
您应该使用 reader.HasRows 属性而不是方法。
回答by Tim Schmelter
Maybe you're getting an exception on reader.Read()
.
也许您在reader.Read()
.
But since HasRows
is a property and not a method. You need to write if(reader.HasRows)
to make it compile.
但因为HasRows
是一个属性而不是一个方法。您需要编写if(reader.HasRows)
以使其编译。
if(reader.HasRows)
{
//MAKES IT HERE
while (reader.Read())
{
//NEVER MAKES IT HERE
}
}
So i'm wondering what the actual code is.
所以我想知道实际的代码是什么。
回答by Al3x3
Just a guess. Maybe you are accessing the reader object in that messagebox shown after reader.HasRows and before reader.Read(). Apparently the Read() method returns true only if the reader has NOT reached the last row (see: https://stackoverflow.com/a/1540626/516481). So if the reader is at the last row it will return false! Maybe the query returns just one row and you change the internal state of the reader object somehow by accessing it (maybe using the debugger) and moving the reader to the last row?
只是一个猜测。也许您正在访问 reader.HasRows 之后和 reader.Read() 之前显示的消息框中的 reader 对象。显然,仅当阅读器未到达最后一行时, Read() 方法才返回 true(请参阅:https: //stackoverflow.com/a/1540626/516481)。所以如果读者在最后一行,它将返回 false!也许查询只返回一行,而您通过访问它(可能使用调试器)并将读取器移动到最后一行来以某种方式更改读取器对象的内部状态?