This is an old revision of the document!
#include "stdafx.h" #include <windows.h> #include <sql.h> #include <sqltypes.h> #include <sqlext.h> #include <iostream> using namespace std; #pragma warning(disable: 4996) int main() { SQLHENV henv; SQLHDBC hdbc; SQLHSTMT hstmt; SQLRETURN retcode; char errormsg[255] = "x"; SQLCHAR * OutConnStr = (SQLCHAR * )malloc(255); SQLSMALLINT * OutConnStrLen = (SQLSMALLINT *)malloc(255); cout << "OdxODBCConnectionTest" << endl; // Allocate environment handle retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); cout << "Alloc ENV retcode:" << retcode << endl; retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC2, 0); cout << "Set Env retcode: " << retcode << endl; retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); cout << "Alloc hdbc retcode: " << retcode << endl; retcode = SQLDriverConnect(hdbc, NULL, (SQLCHAR*) "FILEDSN=c:\\dev\\odx\\tiny\\tiny.dsn", SQL_NTS, (SQLCHAR*) NULL, 0, NULL, 0); cout << "Connect retcode:" << retcode << endl; if (retcode != SQL_SUCCESS) { retcode = SQLError(henv, hdbc, NULL, NULL , NULL, (SQLCHAR*) errormsg, sizeof(errormsg), NULL); cout << errormsg << endl; } retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); cout << "Alloc stmt:" << retcode << endl; retcode = SQLExecDirect(hstmt, (SQLCHAR*) "select mytext from table1 where mytext='bears'", SQL_NTS); cout << "Exec Direct retcode:" << retcode << endl; SQLCHAR mytext[60]; retcode = SQLBindCol(hstmt, 1, SQL_C_CHAR, (SQLPOINTER) &mytext, sizeof(mytext), NULL); while (retcode==0) { retcode = SQLFetch(hstmt); cout << mytext << endl; } SQLFreeHandle(SQL_HANDLE_STMT, hstmt); retcode = SQLDisconnect(hdbc); SQLFreeHandle(SQL_HANDLE_DBC, hdbc); SQLFreeHandle(SQL_HANDLE_ENV, henv); cout << "End of program." << endl; } // end main