我在从 C# ADO.Net 程序连接到 DB2 iSeries 数据库时遇到问题。
我可以使用 ClientAccess“Data Transfer From iSeries”工具从 Windows 机器连接到 DB2/iSeries,设置如下:
IBM i name: 192.168.0.1
User: VCT130
Passwd: pass
File name: CHGDG#VL/AREA(AREA)
它检索了 23 条记录。
我正在尝试从下面总结的简单 C# 程序访问相同的数据:
using IBM.Data.DB2.iSeries;
string conStr= "DataSource=192.168.0.1;UserID=VCT130;Password=pass;Naming=System;LibraryList=QIWS,CHGDG#VL,CHNNL#VL,CHVAT#VL;DefaultCollection=QIWS;" ;
string sql= "SELECT * from CHGDG#VL/Area;" ; // ALSO TRIED ...from CHGDG#VL/Area(AREA);
iDB2Connection conn = new iDB2Connection(conStr);
iDB2Command cmd = new iDB2Command(sql, conn);
iDB2DataReader rdr = cmd.ExecuteReader();
conn 和 cmd 对象创建成功,但 ExecuteReader() 调用失败:
Unhandled Exception: System.InvalidOperationException: The operation cannot complete because the connection is not valid.
at IBM.Data.DB2.iSeries.iDB2Command.verifyConnection()
at IBM.Data.DB2.iSeries.iDB2Command.ExecuteDbDataReader(CommandBehavior behavior)
at IBM.Data.DB2.iSeries.iDB2Command.ExecuteReader()
at test04.DBDB2.read() in
c:\Users\mikeb\projs\ClassLibrary1\ClassLibrary1\DBDB2.cs:line 27
我的第 27 行是对 cmd.ExecuteReader() 的调用
有什么想法吗?
11 月 19 日进一步思考:
[请原谅任何笨拙的礼仪,我是这里的新海报]
- 我错过了什么吗?我在想只有两件事要做
这里错了:- 我的代码。不,太简单了,已通过@MikeWills 的帖子验证
- 我的连接字符串
- 或者还有别的东西吗?
- 重新 IBM.Data.DB2.iSeries.iDB2Command.verifyConnection()
这里有人可以访问代码来说明此方法的作用以及可能导致其失败的原因吗?
最佳答案
已解决。我遗漏了以下内容:
- 需要在 conn= new 之后添加 conn.Open() ....
- 添加 CheckConnectionOnOpen=true;到连接字符串
- 从查询字符串中删除/
关于C# ADO.Net 到 db2 iseries - 连接无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20060502/