简体   繁体   中英

ODBC driver manager Error while calling SQLDriverConnect

I am trying to connect to my driver using SQLDriverConnect. Here is my program.

#include"stdio.h"
#include"stdlib.h"
#include"windows.h"
#include"sqlext.h"
#include"string.h"
#include"sqltypes.h"
#include"conio.h"
SQLHENV henv = (SQLHENV) NULL;


typedef struct 
{
    SDWORD pfNativeError[1];
    SWORD  pcbErrorMsg[1];
    SWORD  cbErrorMsgMax;
    UCHAR  *szErrorMsg;
    UCHAR  *szSqlState;
}ERR_INFO;


RETCODE odbc_Error(SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStmt);

RETCODE odbc_Error(SQLHENV hEnv,SQLHDBC hDbc,SQLHSTMT hStmt)
{
    char  *szBuf;
    int   pt_ch ='.' ;
    int   brac_ch =']' ;
    ERR_INFO  *Err;
    RETCODE st = 0;

    Err = (ERR_INFO *)malloc (sizeof(ERR_INFO));         
    Err->szErrorMsg = (UCHAR* )malloc(200);
    Err->szSqlState = (UCHAR* )malloc(50);
    szBuf = (char *)malloc(600);
    memset (Err->szErrorMsg,'\0',200);
    memset (Err->szSqlState,'\0',50);
    memset (szBuf,'\0',600);

    if (hStmt)
        st = SQLGetDiagRec(SQL_HANDLE_STMT,hStmt,1,Err->szSqlState,
        Err->pfNativeError, Err->szErrorMsg,150,Err->pcbErrorMsg);

    else if (hDbc)
        st = SQLGetDiagRec(SQL_HANDLE_DBC,hDbc,1,Err->szSqlState,
        Err->pfNativeError, Err->szErrorMsg,150,Err->pcbErrorMsg);

    else if (hEnv)
        st = SQLGetDiagRec(SQL_HANDLE_ENV,hEnv,1,Err->szSqlState,
        Err->pfNativeError, Err->szErrorMsg,150,Err->pcbErrorMsg);

    if ((st == SQL_SUCCESS) || (st == SQL_SUCCESS_WITH_INFO))
    {
        sprintf(szBuf," %s  - [%s]\n", (char *)Err->szErrorMsg, Err->szSqlState);
        printf("%s \n",szBuf);
    } 

    free(Err->szErrorMsg);
    free(Err->szSqlState);
    free(Err);
    free(szBuf);

    return SQL_SUCCESS;
}
#define ARRAY_SIZE 8
#define MAX_BINDPARAM1  17
#define DRVC_LEN 1024
struct // We have to support bit, tinyint, binary, varbinary, long varbinary
    {
        SQLSMALLINT SQLType[MAX_BINDPARAM1];
    } CDataArgToSQL1 = 
        {
            SQL_CHAR,SQL_VARCHAR,SQL_DECIMAL,SQL_NUMERIC,SQL_SMALLINT,SQL_INTEGER,SQL_REAL,
            SQL_FLOAT,SQL_DOUBLE,SQL_DATE,SQL_TIME,SQL_TIMESTAMP,SQL_LONGVARCHAR,SQL_BIGINT,
            SQL_CHAR,SQL_VARCHAR,SQL_LONGVARCHAR
    };

int main()
{
    int j =0;
int i = 0;
int  k=0;
    SQLHENV henv;
    SQLHDBC hdbc;
    SQLHSTMT hstmt[1900]; 
    SQLRETURN retcode;
    SQLPOINTER rgbValue = &i;
      SQLRETURN   st     = 0;
      SQLPOINTER    ValuePtr = NULL;
      SQLHWND hwnd=NULL;
       char  buf[450];
 int len;


    char query[400]={'\0'};
    char        connstr[DRVC_LEN]={'\0'};
    char szConnStrOut[DRVC_LEN];
    SWORD       cbConnStrOut;  


    printf("\n\tUsing Data Source :Vivek  \n\n");   

    printf("Test Negative Functionality of SQLDriverConnect: only \n");
    strcpy(connstr,"DSN=VIV_OFF32_64A;UID=super.super;PWD=nedops;");

    retcode = SQLAllocEnv(&henv); 
    if (retcode != SQL_SUCCESS)
    {
          printf("Error in Connection\n");
         odbc_Error(henv,hdbc,NULL);
         getch();

    }

    retcode = SQLAllocConnect(henv, &hdbc); 
    if (retcode != SQL_SUCCESS)
    {
          printf("Error in Connection\n");
         odbc_Error(henv,hdbc,NULL);
         getch();

    }

    retcode = SQLDriverConnect(hdbc,hwnd,(SQLCHAR*)connstr,SQL_NTS,(SQLCHAR*)szConnStrOut,DRVC_LEN,&cbConnStrOut,SQL_DRIVER_PROMPT);
    if (retcode != SQL_SUCCESS)
    {
          printf("Error in Connection\n");
         odbc_Error(henv,hdbc,NULL);
         getch();

    }
    SQLDisconnect(hdbc);
    SQLFreeConnect(hdbc);
    SQLFreeEnv(henv);
}

When i use SQL_DRIVER_NOPROMP , SQL_DRIVER_COMPLETE or SQL_DRIVER_COMPLETE_REQUIRED i got succeess.

my goal is to prompt an window using SQLDriverConnect .

You need to supply a window handle to SQLDriverConnect and you are just passing NULL.

Without a window handle how is it going to display a dialogue.

There are ways to get a window handle if you are running this in a console.

As an aside you are mixing old ODBC APIs with new ones eg, SQLAllocConnect/SQLGetDiagxxx and SQLDriverConnect. If this is a new application you are better writing it using at least the ODBC 3 API so you'd be using SQLAllocHandle, SQLFreeHandle and SQLSetEnvAttr to ask for ODBC 3 behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM