oracle OracleDataReader 出错。错误:无效的操作。连接已关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17062954/
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
Error with OracleDataReader. Error: Invalid operation. The connection is closed
提问by Alek
When I try to assign the reader C# throws an exception:
当我尝试分配读取器时,C# 抛出异常:
Invalid operation. The connection is closed
Invalid operation. The connection is closed
I try to get a result from a query that returns a single cell with an average value inside. cmd is an oraclecomand that i use to insert a row into a table and so far so good. I see the message box next and after that the exception appears.
我尝试从返回单个单元格的查询中获取结果,该单元格内部具有平均值。cmd 是一个 oraclecomand,我用来在表中插入一行,到目前为止一切顺利。我接下来看到消息框,然后出现异常。
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Recipe Rated");
OracleCommand cm = new OracleCommand("select round(avg(rating),1) from rates where id_rec = "+id);
OracleDataReader reader = cm.ExecuteReader();
reader.Read();
textBox5.Text =""+reader.GetInt16(0);
}
回答by Tim Schmelter
You should open the connection and you should also use sql-parameters. Hopefully this is the correct oracle syntax because i cannot test it:
您应该打开连接,并且还应该使用 sql-parameters。希望这是正确的 oracle 语法,因为我无法测试它:
using(var con = new OracleConnection("ConnectionString Here"))
using(var cmd = new OracleCommand("ADD YOUR INSERT/UPDATE/DELETE", con))
{
con.Open();
cmd.ExecuteNonQuery();
using (var cm = new OracleCommand("select round(avg(rating),1)As AvgRating from rates where id_rec = @id", con))
{
cm.Parameters.AddWithValue("@id", id);
using (var reader = cm.ExecuteReader())
{
if (reader.Read())
{
textBox5.Text = reader.GetInt16(0).ToString();
}
}
}
}
Note that i have used the using
-statement to ensure that all unmanaged resources are disposed as soon as possible. It also closes connections (even on error).
请注意,我使用了 -using
语句来确保尽快处理所有非托管资源。它还关闭连接(即使出错)。
Edit: Since you are selecting just a single value i suggest to use ExecuteScalar
:
编辑:由于您只选择一个值,我建议使用ExecuteScalar
:
using (var cm = new OracleCommand("select round(avg(rating),1)As AvgRating from rates where id_rec = @id", con))
{
cm.Parameters.AddWithValue("@id", id);
object avgRating = cm.ExecuteScalar();
if (!(avgRating is DBNull))
{
textBox5.Text = avgRating.ToString();
}
}
回答by FIre Panda
When you use `OracleCommand', you have to associate a valid OracleConnectionobject to it.
当您使用`OracleCommand' 时,您必须将一个有效的OracleConnection对象与其关联。
using (OracleConnection connection = new OracleConnection(connectionString))
{
MessageBox.Show("Recipe Rated");
OracleCommand cm = new OracleCommand("select round(avg(rating),1) from rates where id_rec = "+id);
try
{
cm.Connection = connection;
connection.Open(); //oracle connection object
OracleDataReader reader = cm.ExecuteReader();
reader.Read();
textBox5.Text =""+reader.GetInt16(0);
}
}
Hope this help.
希望这有帮助。
Thanks.
谢谢。