#include #include #include #include typedef struct ODBC3_Handles ODBC3_Handles; struct ODBC3_Handles { SQLHENV env; SQLHDBC dbc; SQLCHAR *zConnStr; }; int main() { SQLRETURN ret; /* ODBC API return status */ ODBC3_Handles *pODBC3conn = NULL; char szConnStrIn[512] = "DSN=sqllogictest"; pODBC3conn = (ODBC3_Handles *)malloc(sizeof(ODBC3_Handles)); if( !pODBC3conn ){ fprintf(stderr, "out of memory at %s:%d\n", __FILE__,__LINE__); return 1; } pODBC3conn->env = SQL_NULL_HENV; pODBC3conn->dbc = SQL_NULL_HDBC; pODBC3conn->zConnStr = NULL; /* Allocate an environment handle */ ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &pODBC3conn->env); if( !SQL_SUCCEEDED(ret) ){ exit(1); } ret = SQLSetEnvAttr(pODBC3conn->env, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0); if (!SQL_SUCCEEDED(ret)) { exit(1); } ret = SQLAllocHandle(SQL_HANDLE_DBC, pODBC3conn->env, &pODBC3conn->dbc); if (!SQL_SUCCEEDED(ret)) { exit(1); } pODBC3conn->zConnStr = (SQLCHAR *)malloc(1024 * sizeof(SQLCHAR)); if (!pODBC3conn->zConnStr) { exit(1); } SQLSMALLINT outStrLen; ret = SQLDriverConnect(pODBC3conn->dbc, NULL, (SQLCHAR *)szConnStrIn, SQL_NTS, pODBC3conn->zConnStr, 1024 * sizeof(SQLCHAR), &outStrLen, SQL_DRIVER_COMPLETE); if (!SQL_SUCCEEDED(ret)) { exit(1); } SQLHSTMT stmt = SQL_NULL_HSTMT; ret = SQLAllocHandle(SQL_HANDLE_STMT, pODBC3conn->dbc, &stmt); if (!SQL_SUCCEEDED(ret) && (ret != SQL_SUCCESS_WITH_INFO)) { exit(1); } ret = SQLExecDirect(stmt, (SQLCHAR *)"select 1, -1", SQL_NTS); if (!SQL_SUCCEEDED(ret) && (ret != SQL_SUCCESS_WITH_INFO)) { exit(1); } SQLSMALLINT columns; /* number of columns in result-set */ ret = SQLNumResultCols(stmt, &columns); if (!SQL_SUCCEEDED(ret) && (ret != SQL_SUCCESS_WITH_INFO)) { exit(1); } ret = SQLFetch(stmt); if (SQL_SUCCEEDED(ret)) { SQLLEN indicator = 0; long int li = 0L; SQLGetData(stmt, 1, SQL_C_SLONG, &li, sizeof(li), &indicator); printf("%ld", li); if (indicator == SQL_NO_TOTAL) { printf("%ld", li); } if (indicator == SQL_NULL_DATA) { printf("%ld", li); } SQLGetData(stmt, 2, SQL_C_SLONG, &li, sizeof(li), &indicator); printf("%ld", li); if (indicator == SQL_NO_TOTAL) { printf("%ld", li); } if (indicator == SQL_NULL_DATA) { printf("%ld", li); } } }