Solved

C++ ODBC

Posted on 2004-04-06
5
836 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

749 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