// Allocate the ODBC Environment and save handle.
retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
// Notify ODBC that this is an ODBC 3.0 application.
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
// Allocate an ODBC connection and connect.
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
retcode = SQLConnect(hdbc1,
"MyDSN", SQL_NTS, "sa", SQL_NTS,
"MyPassWord", SQL_NTS);
// Allocate a statement handle.
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
// Prepare and execute an SQL statement on the statement handle.
// Uses a default result set because no cursor attributes are set.
retcode = SQLPrepare(hstmt1,
"SELECT au_lname from authors", SQL_NTS);
retcode = SQLExecute(hstmt1);
// Simplified result set processing. Bind one column and
// then fetch until SQL_NO_DATA.
retcode = SQLBindCol(hstmt1, 1, SQL_C_CHAR,
szName, MAXNAME, &cbName);
while ( ( retcode = SQLFetch(hstmt1) ) != SQL_NO_DATA )
printf("Name = %s\n", szName);