Search code examples
c#.netdb2ibm-midrange

Connecting to iseries DB2 database with .NET


I've been having some difficulty connecting to an iseries DB2 database from a .net 4.0 application I'm developing. I've been trying to use the IBM.Data.DB2.dll library to connect to it using the following code;

String connectionString = "Database=[DBName];UserID=[UserID];Password=[Password];Server=[ServerName]";
connection = new DB2Connection(connectionString);
connection.Open();

When the connection.Open() command is run I receive the following error:

ERROR [58009] [IBM] SQL30020N Execution of the command or SQL statement failed because of a syntax error in the communication data stream that will affect the successful execution of subsequent commands and SQL statements: Reason Code "0x124C"("0100")"". SQLSTATE=58009

Does anyone know of another way of connecting to this kind of database in .net?


Solution

  • This works for me:

    class Program
    {
        static void Main(string[] args)
        {
            string connString = "DataSource=SYSTEM;UserID=USER;Password=PASSWORD";
            iDB2Connection conn = new iDB2Connection(connString);
            conn.Open();
    
            string cmdString = "CRTPF FILE(TESTLIB/TESTNET) RCDLEN(100)";
            string cmdText = "CALL QSYS.QCMDEXC('" + cmdString + "', " + cmdString.Length.ToString("0000000000") + ".00000" + ")";
    
            iDB2Command cmd = new iDB2Command(cmdText, conn);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
    
            conn.Close();
        }
    }