C#Oracle ODP.net定期查询导致ORA-00604

时间:2012-12-03 10:20:08

标签: c# oracle odp.net

我需要在GetStuff函数中每秒执行相同的查询,但是在一两分钟之后Oracle抛出ORA-00604并且用完了游标,我想在返回结果后我需要以某种方式关闭打开游标。但是我不喜欢每次需要查询时重新连接的想法,我的代码如下:

public MyStuff GetStuff(string paramValue)
{
    OracleCommand command = connection.CreateCommand();
    command.CommandText = "select XXX from YY where param = ? ";
    command.Parameters.Add(":param ", OracleDbType.Varchar2).Value = paramValue;
    IDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
       ...
    }
    command.Dispose();
    return stuff;
 }

1 个答案:

答案 0 :(得分:0)

读者也需要处理:

public MyStuff GetStuff(string paramValue)
{
    using (OracleCommand command = connection.CreateCommand())
    {
        command.CommandText = "select XXX from YY where param = ? ";
        command.Parameters.Add(":param ", OracleDbType.Varchar2).Value = paramValue;
        using (IDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
            ...
            }
        }
        return stuff;
    }
}

查看强烈建议的与IDisposable互动的using statement