【问题标题】:Invalid Cursor State Error C# DB2无效游标状态错误 C# DB2
【发布时间】:2017-10-11 14:28:58
【问题描述】:

我使用 C# ADO.NET OleDb 和 using 语句对 DB2 数据库运行标准查询并得到 System.Data.OleDb.OleDbException (0x80004005): CLI0115E 游标状态无效。 SQLSTATE=24000 当我包含每个 OleDb 对象的 dispose 方法时,查询就会运行。 为什么没有 Dispose 方法会失败?从我研究的所有内容中,使用语句应该为我处理对象。我正在使用 .NET 4.5.1

using (OleDbConnection conn = DBConn.BIPSConn)
{
    using (OleDbCommand cmd = new OleDbCommand(query, conn))
    {
        using (OleDbDataReader rdr = cmd.ExecuteReader())
        {
            while (rdr.Read())
            {
               string orderNumber = rdr.GetString(0).Trim();
               string originCode = rdr.GetString(1).Trim();
               string destinationCode = rdr.GetString(3).Trim();

               Record record = new Record(orderNumber, originCode, destinationCode);
               RecordList.Add(record);                                             
           }
           // for unknown reasons, without these dispose methods we get an Invalid Cursor State error
           rdr.Dispose();
        }
        cmd.Dispose();
    } 
    conn.Dispose();
}

【问题讨论】:

  • 尝试仅使用一个 GetString(),然后将其解析为您的订单号、originCode 和 destinationCode。
  • @AGrammerPro 如何只使用 1 个 GetString?读取器 GetString 方法只需要 1 个 int 用于特定的列序号。
  • 我认为参数是可选的。并且不指定 int 会返回您的所有数据?我可能是非常错误的。对不起
  • 这都是关于RecordList.Add(record); 的——这到底是做什么的?如果它调用数据库,那么你就知道了。

标签: c# ado.net db2


【解决方案1】:

你有一个 using 块——你不想调用 Dispose() 你想调用 Close()。 using 块将负责处理。改为添加对 Close 的调用。

【讨论】:

  • 该代码使用 Dispose() 方法。问题是即使 using 语句应该处理对象,如果没有 Dispose() 方法,它也无法工作。我相信 Dispose() 在内部调用 Close() 所以我也不需要调用 Close()。
猜你喜欢
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多