C++ script doesnt seem to do anything.

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <odbcinst.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>

using namespace std;

int main()
{
      SQLHANDLE hDbConn;
      SQLRETURN retcode;
      char szDSN[] = "SYSDNS";
    char szUID[] = "***";
    char szAuthStr[] = "********";
      retcode = SQLConnect(hDbConn, (UCHAR *)szDSN, SQL_NTS, (UCHAR *)szUID, SQL_NTS, (UCHAR *) szAuthStr, SQL_NTS);
      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
            cout << "it works\n" <<endl;
      } if (retcode == SQL_ERROR) {
            cout << "sql error\n" <<endl;
      } if (retcode == SQL_INVALID_HANDLE) {  
            cout << "sql invalid handle\n" <<endl;
      }
SQLDisconnect(hDbConn);
}

This doesnt want to connect to the db, I'm not sure if its doing anything at all.  If I specify DNS and dont specify DNS I get the same result SQL_INVALID_HANDLE, and I cant seem to find out what is causing it.

Arrrrrrrrrrrrggggggggggggg.
iceman19330Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You heed to allocate the handle first, e.g.

      /*Allocate environment handle */
     retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hDbConn);  
     retcode = SQLConnect(hDbConn, (UCHAR *)szDSN, SQL_NTS, (UCHAR *)szUID, SQL_NTS, (UCHAR *) szAuthStr, SQL_NTS);

0
ikeworkCommented:
hi icemen,

you have to allocate memory first for your connectionhandle "hDbConn"

code taken from:

 http://www.codeproject.com/database/sqlconnect.asp



int main(void)
{
   HENV   hEnv = NULL; // Env Handle from SQLAllocEnv()
   HDBC   hDBC = NULL; // Connection handle
   HSTMT  hStmt = NULL;// Statement handle
   UCHAR  szDSN[SQL_MAX_DSN_LENGTH] = "Test";// Data Source Name buffer
   UCHAR  szUID[10] = "test";// User ID buffer
   UCHAR  szPasswd[10] = "test";// Password buffer
   UCHAR  szModel[128];// Model buffer
   SDWORD cbModel;// Model buffer bytes recieved
   char   buff[9] = "Testing";
   UCHAR  szSqlStr[128]= "INSERT into (Tablename) (ColumnName) Values ('Testing')" ;

   RETCODE retcode;

  //sprintf((char*)szSqlStr,"INSERT into (Tablename)(Columname) Values ('%s')",buff);
  // Allocate memory for ODBC Environment handle
  SQLAllocEnv (&hEnv);

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

  // Connect to the data source "test" 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)
  {
        //  SUCCESS ...



hope it helps :)
ike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
iceman19330Author Commented:
Okay I have moved up in the world of errors.  I now get SQL_ERROR after making both of your suggested changes.

Here is what the error code spit out.

sql error

>--- ERROR -- RC = -1 Reported from odbc2.cpp, line 66 ------------
Segmentation fault

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <odbcinst.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>

using namespace std;

int main()
{
      SQLHANDLE hDbConn;
      SQLRETURN retcode;
      HENV   hEnv = NULL; // Env Handle from SQLAllocEnv()
      char szDSN[] = "SYSDTS";
    char szUID[] = "***";
    char szAuthStr[] = "********";
      SQLAllocEnv (&hEnv);
      SQLAllocConnect (hEnv, &hDbConn);
      retcode = SQLConnect(hDbConn, (UCHAR *)szDSN, SQL_NTS, (UCHAR *)szUID, SQL_NTS, (UCHAR *) szAuthStr, SQL_NTS);
      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
            cout << "it works\n" <<endl;
      } if (retcode == SQL_ERROR) {
            cout << "sql error\n" <<endl;
            cout << retcode <<endl;
      } if (retcode == SQL_INVALID_HANDLE) {  
            cout << "sql invalid handle\n" <<endl;
      }
SQLDisconnect(hDbConn);
}
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

jkrCommented:
I only recommended

nt main()
{
     SQLHANDLE hDbConn;
     SQLRETURN retcode;
     char szDSN[] = "SYSDNS";
    char szUID[] = "***";
    char szAuthStr[] = "********";
      /*Allocate environment handle */
     retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hDbConn);
     retcode = SQLConnect(hDbConn, (UCHAR *)szDSN, SQL_NTS, (UCHAR *)szUID, SQL_NTS, (UCHAR *) szAuthStr, SQL_NTS);
     if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
          cout << "it works\n" <<endl;
     } if (retcode == SQL_ERROR) {
          cout << "sql error\n" <<endl;
     } if (retcode == SQL_INVALID_HANDLE) {  
          cout << "sql invalid handle\n" <<endl;
     }
SQLDisconnect(hDbConn);
}
0
iceman19330Author Commented:
ike, thanks for the pointer to the code page.  That code if I paste it in works fine.  Now I have to figure out how to get the data out of my query.  :)  Any thoughts?
0
jkrCommented:
Time to unsubscribe...
0
ikeworkCommented:
btw: your code calls "SQLDisconnect" twice .. maybe it crashes the second time ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

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.