Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ADO.Net to db2 iseries - connection not valid

I'm having problems connecting to a DB2 iSeries database from a C# ADO.Net program.

I can connect from the Windows box to the DB2/iSeries using the ClientAccess "Data Transfer From iSeries" tool, with these settings:

IBM i name: 192.168.0.1
User: VCT130
Passwd: pass
File name: CHGDG#VL/AREA(AREA)

and it retrieves 23 records.

I'm trying to access the same data from a simple C# program summarised below:

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();

The conn and cmd objects get created OK, but the ExecuteReader() call fails with:

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

My line 27 is the call to cmd.ExecuteReader()

Any ideas please?


19 Nov Further thoughts:

[ Please excuse any clumsy etiquette, I'm a new poster here ]

  • Am I missing something? I'm thinking there are only 2 things to go
    wrong here:
    1. My code. No, too simple, and verified by @MikeWills' post
    2. My connection string
    3. Or could there be something else?


- re IBM.Data.DB2.iSeries.iDB2Command.verifyConnection()

Does someone here have access to the code to say what this method does, and what might be causing it to fail?

like image 381
user3006774 Avatar asked Jan 20 '26 01:01

user3006774


1 Answers

Solved. I was missing the following:

  • need to add conn.Open() after conn= new ....
  • Add CheckConnectionOnOpen=true; to connection string
  • Remove / from query string
like image 193
user3006774 Avatar answered Jan 22 '26 15:01

user3006774



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!