Solved

C++ ODBC

Posted on 2004-04-06
5
835 Views
Last Modified: 2012-06-21
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);
            return;
}
0
Comment
Question by:Dancindan84
  • 2
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 350 total points
ID: 10771928
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));

or:

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

(I don't know exactly SQLPrepare requirement).
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10772019
Yep, AlexFM is correct

The following link has a reference to the SQLPrepare API

http://odbcrouter.com/api/SQLPrepare.shtml

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
0
 

Author Comment

by:Dancindan84
ID: 10773252
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?
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 150 total points
ID: 10773347
Try this

strlen ( (const char * ) szSqlStr )
instead of
strlen(szSqlStr)

Amit
0
 

Author Comment

by:Dancindan84
ID: 10773442
Excellent, that worked. I've posted a follow up question:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20946264.html

If you have any insight into that as well, you could make my week. :-)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question