Open System Services ODBC/MX Client Driver Manual for SQL/MX Release 3.2.1 (H06.26+, J06.15+)
Sample ODBC Application Code
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <sqlext.h>
#include <string.h>
#define UnSignCharCast unsigned char *
#define TRUE 1
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 = (UnSignCharCast )malloc(200);
Err->szSqlState = (UnSignCharCast )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;
}
int main(int argc, char *argv[])
{
RETCODE st = 0;
SQLHENV henv = (SQLHENV) NULL;
SQLHDBC hdbc = (SQLHDBC) NULL;
SQLHDBC hdbc1 = (SQLHDBC) NULL;
Execution and Testing of Sample ODBC Application 55










