• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 848
  • Last Modified:


My problem is that I can't get the code below to work the way I want it to. As shown, it prints out the string sent to it (which for testing purposes was "SELECT * FROM Employee";) and doesn't print out any of the data from the database. If I simply take the comment off of:
//UCHAR   szSqlStr [] = "SELECT * FROM Employee";    // SQL string
and obviously remove the earlier szSqlStr reference, it works fine. I really need to be able to _send_ this function a string to execute. I'm very new to C++ (I've used Java mostly) so be gentle. If any additional info is needed, just let me know. Thanks.

#include <windows.h>
#include <sqlext.h>
#include <stdio.h>
#include <iostream>

void ODBCConnection(UCHAR szSqlStr[])
    cout << szSqlStr;
    HENV    hEnv = NULL;  // Env Handle from SQLAllocEnv()
    HDBC    hDBC = NULL;  // Connection handle
    HSTMT   hStmt = NULL; // Statement handle
    UCHAR   szDSN[SQL_MAX_DSN_LENGTH] = "Employee";    // Data Source Name buffer
    UCHAR*  szUID = NULL;    // User ID buffer
    UCHAR*  szPasswd = NULL;    // Password buffer
    UCHAR   szModel[128];    // Model buffer
    SDWORD  cbModel; // Model buffer bytes recieved
    //UCHAR   szSqlStr [] = "SELECT * FROM Employee";    // SQL string
    RETCODE retcode; // Return code

    // Allocate memory for ODBC Environment handle
    SQLAllocEnv (&hEnv);

    // Allocate memory for the connection handle
    SQLAllocConnect (hEnv, &hDBC);

    // Connect to the data source "db97" using userid and password.
    retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);

    if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
                // Allocate memory for the statement handle
                retcode = SQLAllocStmt (hDBC, &hStmt);

                // Prepare the SQL statement by assigning it to the statement handle
                retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));

                // Execute the SQL statement handle
                retcode = SQLExecute (hStmt);

                // Project only column 2 which is the regions
                SQLBindCol (hStmt, 2, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel);

                // Get row of data from the result set defined above in the statement
                retcode = SQLFetch (hStmt);

                while (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
                    printf ("\t%s\n", szModel); // Print row (model)
                    retcode = SQLFetch (hStmt); // Fetch next row from result set

                    // Free the allocated statement handle
                    SQLFreeStmt (hStmt, SQL_DROP);

                    // Disconnect from datasource
                    SQLDisconnect (hDBC);

            // Free the allocated connection handle
            SQLFreeConnect (hDBC);

            // Free the allocated ODBC environment handle
            SQLFreeEnv (hEnv);
  • 2
  • 2
2 Solutions
My guess is that this is the line which makes problems:

retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));

When you write:
UCHAR   szSqlStr [] = "SELECT * FROM Employee";

compiler knows exactly what is sizeof (szSqlStr). But when szSqlStr is function parameter, compiler doesn't know string size and sizeof (szSqlStr) is set to 4 (pointer size in Win32). To correct this, use:

retcode = SQLPrepare (hStmt, szSqlStr, strlen(szSqlStr));


retcode = SQLPrepare (hStmt, szSqlStr, strlen(szSqlStr) + 1);

(I don't know exactly SQLPrepare requirement).
Yep, AlexFM is correct

The following link has a reference to the SQLPrepare API


It shows that the third parameter is indeed the length of the SQL text string

So, u would have to use strlen () as AlexFM suggested
Dancindan84Author Commented:
When I change sizeof to strlen I get the following error:

invalid conversion from `UCHAR*' to `const char*'

Apparently strlen doesn't want to take a UCHAR. Any other way to get the length of it?
Try this

strlen ( (const char * ) szSqlStr )
instead of

Dancindan84Author Commented:
Excellent, that worked. I've posted a follow up question:

If you have any insight into that as well, you could make my week. :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now