ASA Programming Guide
ODBC Programming
ODBC handles
The following is a simple ODBC program that connects to the Adaptive Server Anywhere sample database and immediately disconnects.
You can find this sample as Samples\ASA\ODBCConnect\odbcconnect.cpp in your SQL Anywhere directory.
#include <stdio.h> #include "ntodbc.h" int main(int argc, char* argv[]) { SQLHENV env; SQLHDBC dbc; SQLRETURN retcode; retcode = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env ); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { printf( "env allocated\n" ); /* Set the ODBC version environment attribute */ retcode = SQLSetEnvAttr( env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); retcode = SQLAllocHandle( SQL_HANDLE_DBC, env, &dbc ); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { printf( "dbc allocated\n" ); retcode = SQLConnect( dbc, (SQLCHAR*) "ASA 9.0 Sample", SQL_NTS, (SQLCHAR* ) "DBA", SQL_NTS, (SQLCHAR*) "SQL", SQL_NTS ); if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { printf( "Successfully connected\n" ); } SQLDisconnect( dbc ); } SQLFreeHandle( SQL_HANDLE_DBC, dbc ); } SQLFreeHandle( SQL_HANDLE_ENV, env ); return 0; }