// Start of code.
#include <windows.h>
#include <stdio.h>
#include <sql.h>
#include <odbcinst.h>
#include <sqlext.h>
#include "odbcss.h"
#include <iostream>
using namespace std;
void main()
{
const int MAX_STR_LEN = 256;
SQLHENV hEnv;
SQLHDBC hConn;
SQLHSTMT hStmt;
SQLRETURN nRet;
SQLHDESC hDescIRD;
SQLINTEGER iResultDesc;
SQLINTEGER iTableDesc;
SQLCHAR szTableName[MAX_STR_LEN];
char szNum[] = "CA";
SQLINTEGER nIndicator = SQL_NTS;
// Allocate environment.
nRet = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
// Set the environment attributes. Setting the odbc version must be the first thing done.
nRet = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, NULL);
// Allocate connection.
nRet = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hConn);
//Change the Connection string accordingly.
nRet = SQLDriverConnect(hConn, NULL, (SQLCHAR*)"DSN=DSN NAME;UID=User ID;PWD=Password;", SQL_NTS,NULL, SQL_NTS, NULL, SQL_DRIVER_NOPROMPT);
// Allocate a statement handle.
nRet = SQLAllocHandle(SQL_HANDLE_STMT, hConn, &hStmt);
// Force a non Firehose cursor
nRet = SQLSetStmtAttr(hStmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER)SQL_CURSOR_KEYSET_DRIVEN, SQL_IS_UINTEGER);
nRet = SQLSetStmtAttr(hStmt, SQL_ATTR_CONCURRENCY, (SQLPOINTER)SQL_CONCUR_VALUES ,SQL_IS_UINTEGER);
nRet = SQLSetStmtAttr(hStmt, SQL_SOPT_SS_DEFER_PREPARE, (SQLPOINTER)SQL_DP_ON ,SQL_IS_UINTEGER);
// Prepare some SQL with a "WHERE" clause.
nRet = SQLPrepare(hStmt, (SQLCHAR*)"SELECT * from Authors WHERE state =?", SQL_NTS);
nRet = SQLBindParameter(hStmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,20,0,
&szNum,sizeof(szNum),&nIndicator);
// nRet = SQLExecute(hStmt);
// Clear the memory for debugging.
ZeroMemory(szTableName, MAX_STR_LEN);
// Get handle to IRD.
nRet = SQLGetStmtAttr(hStmt, SQL_ATTR_IMP_ROW_DESC, &hDescIRD, sizeof(SQLHDESC), &iResultDesc);
// Try getting the table name directly from the IRD for first column.
nRet = SQLGetDescField(hDescIRD, 1, SQL_DESC_BASE_TABLE_NAME, szTableName, MAX_STR_LEN, &iTableDesc);
cout << "Table name: " << szTableName << endl;
}
// End of code.