Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    6

    Unanswered: DB2 CLI Connection Handle Fails

    Hi All,

    I am trying to write a simple DB2 CLI application but somehow there is always an SQL_ERROR while allocating Connection Handle. Can someone please have a look and help me figure out the issue ( I am using Visual Studio Express, DB2 9). The code is pasted below:


    /* Standard C/C++ Headers */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    /* DB2 ODBC Header */
    #include <sqlcli1.h>

    /*Global Constants*/
    #define MAX_SQL_QUERY_LENGTH 32760
    #define MAX_UID_LENGTH 18
    #define MAX_PWD_LENGTH 30

    int CommandLineArgs(int argc,
    char* argv[],
    char dbAlias[],
    char userName[],
    char password[]);

    void GetCLIErrorInfo(SQLSMALLINT handleType,
    SQLHANDLE handleName);

    /* Main Program
    */int main(int argc, char* argv[]) {

    /* Handles */
    SQLHENV henv = SQL_NULL_HENV; /*Enviroment Handle*/
    SQLHDBC hdbc = SQL_NULL_HDBC; /*Connection Handle*/
    SQLHSTMT hstmt= SQL_NULL_HSTMT; /*Statement Handle*/

    /* Local variables */
    SQLCHAR sqlQuery [MAX_SQL_QUERY_LENGTH + 1];
    char dbAlias [SQL_MAX_DSN_LENGTH + 1];
    char userName [MAX_UID_LENGTH + 1];
    char password [MAX_PWD_LENGTH + 1];
    int returnCode;

    SQLRETURN sqlReturnCode = SQL_SUCCESS;

    returnCode = CommandLineArgs ( argc,
    argv,
    dbAlias,
    userName,
    password);
    if (returnCode != 0) {
    return returnCode;
    }

    /*Allocate an Environment Handle */
    sqlReturnCode = SQLAllocHandle(SQL_HANDLE_ENV,
    SQL_NULL_HANDLE,
    &henv);

    if (sqlReturnCode != SQL_SUCCESS)
    {
    if (sqlReturnCode != SQL_SUCCESS) {
    GetCLIErrorInfo(SQL_HANDLE_DBC,henv);
    }
    }

    /*Allocate an Connection Handle */
    sqlReturnCode = SQLAllocHandle(SQL_HANDLE_DBC,
    henv,
    &hdbc);

    if (sqlReturnCode != SQL_SUCCESS) {
    // GetCLIErrorInfo(SQL_HANDLE_DBC,hdbc); /*I guess this was wrong*/

    GetCLIErrorInfo(SQL_HANDLE_ENV,henv);

    }

    return 0;
    }

    int CommandLineArgs(int argc,
    char* argv[],
    char dbAlias[],
    char userName[],
    char password[]) {

    int returnCode = 0;

    switch (argc) {
    case 4 :
    strcpy(dbAlias, argv[1]);
    strcpy(userName,argv[2]);
    strcpy(password,argv[3]);
    break;
    default:
    printf("\nUSAGE: %s [dbAlias [userid passwd]]\n", argv[0]);
    returnCode = 1;
    break;
    }

    return returnCode;
    }


    void GetCLIErrorInfo(SQLSMALLINT handleType,
    SQLHANDLE handleName) {

    SQLWCHAR message [SQL_MAX_MESSAGE_LENGTH + 1] = {NULL};
    SQLWCHAR sqlState[SQL_SQLSTATE_SIZE + 1] = {NULL};
    long sqlReturnCode = 0;
    SQLSMALLINT length = 0;
    SQLSMALLINT index = 1;
    int returnCode = 0;

    while (SQLGetDiagRec(handleType,
    handleName,
    index,
    sqlState,
    &sqlReturnCode,
    message,
    SQL_MAX_MESSAGE_LENGTH + 1,
    &length) != SQL_NO_DATA) {

    printf("\n SQLSTATE = %s\n", sqlState);
    printf(" Native Error Code = %ld\n", sqlReturnCode);
    printf("%s\n", message);
    index ++;
    }

    }
    Attached Thumbnails Attached Thumbnails ODBC_CLI.jpg   ODBC_CLI_Output.jpg  
    Last edited by BumbleI3ee; 01-16-13 at 13:23. Reason: Uploaded a screenshot of Error State

  2. #2
    Join Date
    Jan 2013
    Posts
    6
    I have changed a couple of things around and I've figured out that the error is SQLSTATE HY010 , [Microsoft][ODBC Driver Manager] Function sequence error! As far as I can see, The environment handle is correctly setup before I am trying to allocate the Connection Handle. Is there anything wrong with what I'm trying to do.

    I have a feeling that this question is more of a 'C' question, than a Db2 question now, but any help would be appreciated. Programming in C after a stint in COBOL and zOS has it's effects!

    Any help would be appreciated.
    Thanks!

  3. #3
    Join Date
    Apr 2006
    Location
    Belgium
    Posts
    2,514
    Provided Answers: 11
    you could have a look at the ibm supplied samples : below sqllib/samples you have samples for many coding languages....
    Best Regards, Guy Przytula
    Database Software Consultant
    Good DBAs are not formed in a week or a month. They are created little by little, day by day. Protracted and patient effort is needed to develop good DBAs.
    Spoon feeding : To treat (another) in a way that discourages independent thought or action, as by overindulgence.
    DB2 UDB LUW Certified V7-V8-V9-V9.7-V10.1-V10.5 DB Admin - Advanced DBA -Dprop..
    Information Server Datastage Certified
    http://www.infocura.be

  4. #4
    Join Date
    Jan 2013
    Posts
    6
    Hi,

    Thanks for the Reply. In fact I have been looking at the sample codes under SQLLIB\samples\cli and the IBM Manuals "Call Level Interface Guide and Reference Vols 1 & 2" as I've just started with ODBC/CLI and DB2 programming.

    I tried linking the source with db2cli.lib and db2api.lib (Connectivity cheat sheet for Application Developers (Part 4)) but somehow did not realize that the way I was doing it was incorrect.

    I have managed to isolate the issue and the application runs fine as of now, although I still have some doubts and I'll get back if I am not able to figure them out!

    Thanks for the reply!
    Regards.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •