【发布时间】: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);的——这到底是做什么的?如果它调用数据库,那么你就知道了。