?
Solved

C++ script doesnt seem to do anything.

Posted on 2006-04-20
7
Medium Priority
?
285 Views
Last Modified: 2010-04-02
#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.
0
Comment
Question by:iceman19330
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16501274
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
 
LVL 20

Accepted Solution

by:
ikework earned 2000 total points
ID: 16501276
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
 

Author Comment

by:iceman19330
ID: 16501517
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:jkr
ID: 16501563
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
 

Author Comment

by:iceman19330
ID: 16501633
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
 
LVL 86

Expert Comment

by:jkr
ID: 16501789
Time to unsubscribe...
0
 
LVL 20

Expert Comment

by:ikework
ID: 16502119
btw: your code calls "SQLDisconnect" twice .. maybe it crashes the second time ...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.
Suggested Courses

839 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