首页 > Linux操作系统 > Linux操作系统 > 程序连接Timesten
test.cc
#ifdef WIN32
#include
#else
#include
#endif
#include
#include
#include
#include
#include
void CheckReturnCode(SQLRETURN rc, SQLHENV henv,
SQLHDBC hdbc, SQLHSTMT hstmt,
char* msg, char *filename,
int lineno);
int main(int argc, char** argv)
{
SQLRETURN rc = SQL_SUCCESS;
/* General return code for the API */
SQLHENV hEnv = SQL_NULL_HENV;
/* Environment handle */
SQLHDBC hDbc = SQL_NULL_HDBC;
/* Connection handle */
SQLHSTMT hStmt = SQL_NULL_HSTMT;
/* Statement handle */
SQLCHAR ConnOut[255];
/* Buffer for completed connection string */
SQLSMALLINT connOutLen;
/* number of bytes returned in ConnOut */
SQLCHAR *ConnString = (SQLCHAR *)
"DSN=test_cs";
/* Connection attributes */
rc = SQLAllocEnv(&hEnv);
if (rc != SQL_SUCCESS) {
fprintf(stderr,"Unable to allocate an "
"environment handle\n");
exit(1);
}
rc = SQLAllocConnect(hEnv, &hDbc);
CheckReturnCode(rc, hEnv, SQL_NULL_HDBC,
SQL_NULL_HSTMT,
"Unable to allocate a "
"connection handle\n",
__FILE__, __LINE__);
#if 1
rc = SQLDriverConnect(hDbc, NULL,
ConnString, SQL_NTS,
ConnOut, 255,
&connOutLen,
SQL_DRIVER_NOPROMPT);
#else
rc = SQLConnect(hDbc, ConnString,
SQL_NTS,(SQLCHAR*)"", SQL_NTS,
(SQLCHAR*)"", SQL_NTS);
printf("\n\nSQLConnect():%d\n",rc);
#endif
CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT,
"Error in connecting to the"
" driver\n",
__FILE__, __LINE__);
rc = SQLAllocStmt(hDbc, &hStmt);
CheckReturnCode(rc, hEnv, hDbc, SQL_NULL_HSTMT,
"Unable to allocate a "
"statement handle\n",
__FILE__,__LINE__);
/* Your application code here */
if (hStmt != SQL_NULL_HSTMT)
{
rc = SQLFreeStmt(hStmt, SQL_DROP);
CheckReturnCode(rc, hEnv, hDbc, hStmt,
"Unable to free the "
"statement handle\n",
__FILE__, __LINE__);
}
if (hDbc != SQL_NULL_HDBC)
{
rc = SQLDisconnect(hDbc);
CheckReturnCode(rc, hEnv, hDbc,
SQL_NULL_HSTMT,
"Unable to close the "
"connection\n",
__FILE__, __LINE__);
rc = SQLFreeConnect(hDbc);
CheckReturnCode(rc, hEnv, hDbc,
SQL_NULL_HSTMT,
"Unable to free the "
"connection handle\n",
__FILE__, __LINE__);
}
if (hEnv != SQL_NULL_HENV)
{
rc = SQLFreeEnv(hEnv);
CheckReturnCode(rc, hEnv, SQL_NULL_HDBC,
SQL_NULL_HSTMT,
"Unable to free the "
"environment handle\n",
__FILE__, __LINE__);
}
}
void CheckReturnCode(SQLRETURN rc, SQLHENV henv,
SQLHDBC hdbc, SQLHSTMT hstmt,
char* msg, char *filename,
int lineno)
{
#define MSG_LNG 512
SQLCHAR szSqlState[MSG_LNG];
/* SQL state string */
SQLINTEGER pfNativeError;
/* Native error code */
SQLCHAR szErrorMsg[MSG_LNG];
/* Error msg text buffer pointer */
SQLSMALLINT pcbErrorMsg;
/* Error msg text Available bytes */
SQLRETURN ret = SQL_SUCCESS;
if (rc != SQL_SUCCESS && rc != SQL_NO_DATA_FOUND )
{
if (rc != SQL_SUCCESS_WITH_INFO)
{
/*
* It's not just a warning
*/
fprintf(stderr, "*** ERROR in %s, line %d:"
" %s\n",
filename, lineno, msg);
}
/*
* Now see why the error/warning occurred
*/
while (ret == SQL_SUCCESS ||
ret == SQL_SUCCESS_WITH_INFO)
{
ret = SQLError(henv, hdbc, hstmt,
szSqlState, &pfNativeError,
szErrorMsg, MSG_LNG,
&pcbErrorMsg);
switch (ret)
{
case SQL_SUCCESS:
fprintf(stderr, "*** %s\n"
"*** ODBC Error/Warning = %s, "
"TimesTen Error/Warning "
" = %d\n",
szErrorMsg, szSqlState,
pfNativeError);
break;
case SQL_SUCCESS_WITH_INFO:
fprintf(stderr, "*** Call to SQLError"
" failed with return code of "
"SQL_SUCCESS_WITH_INFO.\n "
"*** Need to increase size of"
" message buffer.\n");
break;
case SQL_INVALID_HANDLE:
fprintf(stderr, "*** Call to SQLError"
" failed with return code of "
"SQL_INVALID_HANDLE.\n");
break;
case SQL_ERROR:
fprintf(stderr, "*** Call to SQLError"
" failed with return code of "
"SQL_ERROR.\n");
break;
case SQL_NO_DATA_FOUND:
break;
} /* switch */
} /* while */
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26107758/viewspace-714433/ ,如需转载,请注明出处,否则将追究法律责任。