oracle OracleDataReader 抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4629663/
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
OracleDataReader throws exception
提问by xbonez
In my current applicaltion, I need to execute a SQL query, and get the count of the results returned before proceeding. This is my code below:
在我当前的应用程序中,我需要执行 SQL 查询,并在继续之前获取返回的结果计数。这是我的代码如下:
if (GetCount(reader) == 1)
{
reader.Read();
Console.WriteLine(reader["field1"]);
Console.WriteLine(reader["field2"]);
}
And this is the GetCount()
method
这就是GetCount()
方法
public static int GetCount(OracleDataReader reader)
{
int count = 0;
while (reader.Read())
{
count++;
}
return count;
}
After receiving the count of the results, when it enters the if code block, an exception gets thrown out stating:
收到结果的计数后,当它进入 if 代码块时,会抛出一个异常,说明:
[System.InvalidOperationException] = {"Operation is not valid due to the current state of the object."}
However, if I do not get the count, it works fine.
但是,如果我没有得到计数,它就可以正常工作。
I think that since my GetCount function has reader.Read(), I need to somehow reset the reader before reading the values? Honestly, I'm stumped. Any suggestions?
我认为由于我的 GetCount 函数有 reader.Read(),我需要在读取值之前以某种方式重置读取器?老实说,我很难过。有什么建议?
Edit;
In the first code block, I get the same exception even if I comment out reader.Read();
编辑; 在第一个代码块中,即使我注释掉,我也会得到相同的异常reader.Read();
回答by Seth Moore
After GetCount()
you've iterated through your reader. You need to do something with your values before you call the next reader.Read()
or you'll just lose them. Basically you're trying to iterate through your reader twice, which you can't do.
在GetCount()
您遍历阅读器之后。在你打电话给下一个之前,你需要对你的价值观做一些事情,reader.Read()
否则你就会失去它们。基本上,您正在尝试对您的阅读器进行两次迭代,这是您无法做到的。
You could pass a DataSet
into GetCount()
by reference and populate it, then reference the DataSet
in your main code block. However, if you do put it into a DataSet
, you don't really need to count anything yourself, since a DataSet
has sufficient properties.
你可以传递DataSet
到GetCount()
引用和填充它,然后引用DataSet
在主代码块。但是,如果您确实将其放入 a 中DataSet
,则您实际上不需要自己计算任何东西,因为 aDataSet
具有足够的属性。
Example:
例子:
myDataset.Tables[0].Rows.Count
EDIT: Try this
编辑:试试这个
OracleDataAdapter adapter = new OracleDataAdapter(yourSQLQuery,yourConnString);
DataSet data = new DataSet();
adapter.Fill(data);
if (data.Tables.Count != 0)
{
if (data.Tables[0].Rows.Count == 1)
{
...
}
}
回答by Mike Ohlsen
Since you are using a reader, you reached the end, and it would need to be reset to go back to the beginning.
由于您使用的是阅读器,因此您已到达终点,需要重新设置才能返回起点。
It looks like you don't really care what the count is, just that it has rows. SO you could just use the HasRows Property.
看起来你并不真正关心计数是多少,只是它有行。所以你可以只使用 HasRows 属性。
if (reader.HasRows)
{
reader.Read();
Console.WriteLine(reader["field1"]);
Console.WriteLine(reader["field2"]);
}
This would not work if you really need the number of rows, but in this example, it would not matter.
如果您确实需要行数,这将不起作用,但在此示例中,这无关紧要。
http://msdn.microsoft.com/en-us/library/system.data.oracleclient.oracledatareader.aspx
http://msdn.microsoft.com/en-us/library/system.data.oracleclient.oracledatareader.aspx