SQL_NULL_HANDLE, &hdlEnv);
Set ODBC Driver version:
SQLSetEnvAttr(hdlEnv,SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODB
Set the connection handle:
SQLAllocHandle(SQL_HANDLE_DBC,
hdlEnv, &hdlConn);
Connect to the database:
SQLConnect(hdlConn, (SQLCHAR*)dsnName,SQL_NTS,(SQL CHAR*) userID,SQL_NTS, (SQLCHAR*)passwd, SQL_NTS);
3. Choose Driver
•DSN – Data Source Name
•Open the GUI ODBC Administrator (ODBCConfig)
•Choose the appropriate ODBC driver
•Provide a meaningful name to the DSN
•Specify the server and the host string (host string is required if the server is running on a different machine)
4. Query the Database
Querying the database involves the following steps:
–Creating a Statement
SQLAllocHandle(SQL_HANDLE_STMT
, hdlDbc, &hdlStmt);
It allocates the memory for the statement handle. The database handle obtained during connection phase is passed as the second argument.
– Executing a Query
SQLExecDirect(hdlStmt, stmt, SQL_NTS);
It executes the query, which is passed in as SQLCHAR* in the second argument.
5. Extract the Data out of the Executed Query
SQLGetData(hStmt,colNum,type,retVal,buffLength,&cbData);
It extracts data from table as void* data and places it in retVal
colNum refers to the column number provided in the SELECT statement in SQLExecDirect()
Type is one of the standard ODBC data types
example: DT_STRING Ã for a string data type
DT_DOUBLE Ã for a double data type
buffLength is the estimated size of the expected data
cbData is the actual size of the data
6. Traverse Through the Results
SQLFetch(hStmt);
Fetches the next record
hStmt is the statement handle obtained using SQLAllocHandle
If a record is available, It returns SQL_SUCCEEDED
7. Close the Statement and the Connection
SQLFreeHandle(SQL_HANDLE_STMT,hdlStmt);
It closes and de-allocates the memory reserved for the statement handle
SQLFreeHandle(SQL_HANDLE_DBC, hdlConn);
It disconnects and de-allocates the memory reserved for the connection handle
SQLFreeHandle(SQL_HANDLE_ENV, hdlEnv);
It de-allocates the memory occupied by the environment handle