3

我在从 C# ADO.Net 程序连接到 DB2 iSeries 数据库时遇到问题。

我可以使用 ClientAccess“从 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 日进一步的想法:

[请原谅任何笨拙的礼仪,我是这里的新海报]

  • 我错过了什么吗?我认为这里只有两件事会
    出错:
    1. 我的代码。不,太简单了,由@MikeWills 的帖子验证
    2. 我的连接字符串
    3. 或者还能有别的东西吗?


- 重新 IBM.Data.DB2.iSeries.iDB2Command.verifyConnection()

这里有人可以访问代码来说明此方法的作用以及可能导致它失败的原因吗?

4

2 回答 2

3

解决了。我错过了以下内容:

  • 需要在 conn=new ... 之后添加 conn.Open()
  • 添加 CheckConnectionOnOpen=true; 到连接字符串
  • 从查询字符串中删除 /
于 2013-11-28T11:25:44.320 回答
0

看看这个样本是否能帮助你。我编写了一个库来使 IBM 服务器连接更容易,这是该库的一个非常旧的版本。我需要再次将新版本公开。

更新:我的连接字符串是:DataSource=127.0.0.1;DefaultCollection=LIBRARY;Naming=sql;UserID=USER;Password=PASSWORD;

于 2013-11-19T03:49:21.873 回答