Solved

C++ ODBC

Posted on 2004-04-06
5
825 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now