OracleDataReader 出错.错误:无效的操作.连接已关闭 [英] Error with OracleDataReader. Error: Invalid operation. The connection is closed
本文介绍了OracleDataReader 出错.错误:无效的操作.连接已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试分配读取器时,C# 抛出异常:
When I try to assign the reader C# throws an exception:
无效操作.连接已关闭
我尝试从返回单个单元格的查询中获取结果,其中包含平均值.cmd 是一个 oraclecomand,我用来在表中插入一行,到目前为止一切顺利.我看到旁边的消息框,然后出现异常.
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.
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);
}
推荐答案
当您使用 `OracleCommand',您必须关联一个有效的OracleConnection 对象.
When you use `OracleCommand', you have to associate a valid OracleConnection object to it.
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);
}
}
希望对您有所帮助.
谢谢.
这篇关于OracleDataReader 出错.错误:无效的操作.连接已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文