/**********************************************************************
* FILENAME : BrowseConnect.c
*
* DESCRIPTION :
* Example uses SQLBrowseConnect to illustrate an iterative method of
* discovering and enumerating the attributes and attribute values
* required to connect to a data source.
*
* ODBC USAGE :
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>
#include <string.h>
#include "util.c"
#define BRWS_LEN 1000
int main () {
SQLHENV henv = SQL_NULL_HENV; // Environment
SQLHDBC hdbc = SQL_NULL_HDBC; // Connection handle
SQLHSTMT hstmt = SQL_NULL_HSTMT; // Statement handle
SQLRETURN retcode;
SQLCHAR strConnIn[BRWS_LEN], strConnOut[BRWS_LEN];
SQLSMALLINT lenConnOut;
// Allocate the environment handle.
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_ENV)",
henv, SQL_HANDLE_ENV);
// Set the version environment attribute.
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER*)SQL_OV_ODBC3, 0);
CHECK_ERROR(retcode, "SQLSetEnvAttr(SQL_ATTR_ODBC_VERSION)",
henv, SQL_HANDLE_ENV);
// Allocate the connection handle.
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_DBC)",
hdbc, SQL_HANDLE_DBC);
// Call SQLBrowseConnect until it returns a value other than SQL_NEED_DATA.
// The initial call to SQLBrowseConnect is a DSN. If SQL_NEED_DATA is
// returned, calls getStr (not shown) to build a dialog from the values in
// strConnOut. The user-supplied values are passed in the next call
// to SQLBrowseConnect.
strcpy((char*)strConnIn, "DSN=DATASOURCE"); // First call
do {
retcode = SQLBrowseConnect(hdbc, strConnIn, SQL_NTS, strConnOut,
BRWS_LEN, &lenConnOut);
if (retcode == SQL_NEED_DATA) {
printf ("\nIn Str : %s\n", strConnIn);
printf ("\nOut Str : %s\n", strConnOut);
getStr ("\nEnter Next Value", strConnIn, BRWS_LEN, 'N');
}
} while (retcode == SQL_NEED_DATA);
CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_DBC)",
hdbc, SQL_HANDLE_DBC);
exit:
printf ("\nComplete.\n");
// Free handles
// Connection
if (hdbc != SQL_NULL_HDBC) {
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
}
// Environment
if (henv != SQL_NULL_HENV)
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return 0;
}
Further information