【发布时间】:2011-01-07 19:53:43
【问题描述】:
在我当前的应用程序中,我需要执行 SQL 查询,并在继续之前获取返回结果的计数。这是我下面的代码:
if (GetCount(reader) == 1)
{
reader.Read();
Console.WriteLine(reader["field1"]);
Console.WriteLine(reader["field2"]);
}
这就是GetCount() 方法
public static int GetCount(OracleDataReader reader)
{
int count = 0;
while (reader.Read())
{
count++;
}
return count;
}
收到结果计数后,进入if代码块,抛出异常:
[System.InvalidOperationException] = {"Operation is not valid due to the current state of the object."}
但是,如果我没有得到计数,它可以正常工作。
我认为由于我的 GetCount 函数具有 reader.Read(),我需要在读取值之前以某种方式重置读取器吗?老实说,我很难过。有什么建议吗?
编辑;
在第一个代码块中,即使我注释掉 reader.Read();,我也会得到相同的异常
【问题讨论】: