Solved

C++ ODBC

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

895 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

18 Experts available now in Live!

Get 1:1 Help Now