0
votes

I am trying to connect to a remote db2 server using my code in c++. I am able to connect to the db2 server using the db2CLP to connect the server. I have configured the db2cli using the following commands:

db2cli writecfg add -dsn alias -database BLUDB -host hostname -port 50000

and even validated it using

db2cli validate -dsn alias -connect -user userid -passwd password

I am getting an error code of : Native Error Code = -1531 when i use the function

SQLConnect(hdbc,
        (SQLWCHAR *)db1Alias,
        SQL_NTS,
        (SQLWCHAR *)user,
        SQL_NTS,
        (SQLWCHAR *)pswd,
        SQL_NTS); 

And no error messages. Can anyone point out if I am doing something wrong?

I have checked the error codes in IBM's page and did not find -1531 in its list. (https://www.ibm.com/support/knowledgecenter/en/SSEPEK_11.0.0/codes/src/tpc/db2z_n.html)

Here is the snippet of the code i am using : I had picked this up from ibm's sample section(https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.apdv.sample.doc/doc/cli/s-dbconn-c.html)

#define DBC_HANDLE_CHECK(hdbc, cliRC)              \
if (cliRC != SQL_SUCCESS)                          \
{                                                  \

    SQLWCHAR message[SQL_MAX_MESSAGE_LENGTH + 1]; \
    SQLWCHAR sqlstate[SQL_SQLSTATE_SIZE + 1]; \
    SQLINTEGER sqlcode; \
    SQLSMALLINT length, i; \

    i = 1; \

    /* get multiple field settings of diagnostic record */
    while (SQLGetDiagRec(SQL_HANDLE_DBC, \
        hdbc, \ 
        i, \
        sqlstate, \
        &sqlcode, \
        message, \
        SQL_MAX_MESSAGE_LENGTH + 1, \
        &length) == SQL_SUCCESS) \
    { \
        printf("\n  SQLSTATE          = %s\n", sqlstate); \
        printf("  Native Error Code = %d\n", sqlcode); \
        printf("Error Meassgaes:%s\n", message); \
        i++; \
    } \

    printf("-------------------------\n"); \
  if (rc != 0) return rc;                          \
}
/* allocate a database connection handle */
    cliRC = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
    ENV_HANDLE_CHECK(henv, cliRC);

    printf("\n  Connecting to the database %s ...\n", db1Alias);

    /* connect to the database */
    cliRC = SQLConnect(hdbc,
        (SQLWCHAR *)db1Alias,
        SQL_NTS,
        (SQLWCHAR *)user,
        SQL_NTS,
        (SQLWCHAR *)pswd,
        SQL_NTS);
    DBC_HANDLE_CHECK(hdbc, cliRC);

Actual result

  Native Error Code = -1531
Error Meassgaes:[
-------------------------

Expected :the connection should be established.

2
One link points to Db2 for z/OS, one to Db2 LUW. What product and version are you trying to connect to? - data_henrik
i am connecting to db2 LUW. - Abhin Jose

2 Answers

0
votes

Here is the documentation for SQL01531N. -1531 is transformed into the SQL+5 digits+N (negative), hence SQL01531N.

The db2dsdriver.cfg configuration file contains database directory information and client configuration parameters for use by some IBM data server clients and drivers.

The CLI/ODBC initialization file (db2cli.ini) contains various keywords and values that can be used to configure the behavior of CLI and the applications using CLI.

This message is returned when a data source name is specified in a connection string with the "DSN" connection keyword but the specified data source name cannot be found in either of the following locations:

  • Specified with the "dsn alias" configuration keyword in the db2dsdriver.cfg configuration file
  • Specified as a section header in the db2cli.ini configuration file

How and where is db1Alias set in your code? What is the DSN alias name that is configured? Fix that and it should work.

0
votes

The fix was updating the Character set of the application. Previously it was set Unicode.

I went to solution properties->Genral->Character Set and set its value to Not Set and it started working.