/**********************************************************************
* FILENAME : ListConnectionAttributes.c
*
* DESCRIPTION :
* Example uses SQLGetConnectAttr() to obtain and display connection
* attributes
*
* ODBC USAGE :
* SQLGetConnectAttr - to obtain values for attributes:
*
* SQL_ATTR_ACCESS_MODE
* SQL_ATTR_AUTO_IPD
* SQL_ATTR_AUTOCOMMIT
* SQL_ATTR_CONNECTION_DEAD
* SQL_ATTR_CONNECTION_TIMEOUT
* SQL_ATTR_LOGIN_TIMEOUT
* SQL_ATTR_METADATA_ID
* SQL_ATTR_PACKET_SIZE
* SQL_ATTR_TRACE
*
* SQL_ATTR_CURRENT_CATALOG
* SQL_ATTR_TRACEFILE
* SQL_ATTR_TRANSLATE_LIB
*/
#include <stdio.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>
#include <string.h>
#include "util.c"
#define STRLEN 32
int main () {
SQLHENV henv = SQL_NULL_HENV; // Environment handle
SQLHDBC hdbc = SQL_NULL_HDBC; // Connection handle
SQLRETURN retcode; // Return status
SQLUINTEGER uIntVal; // Unsigned int attribute values
SQLCHAR cStr[STRLEN]; // char attribute values
// Allocate an environment handle
retcode=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_ENV)",
henv, SQL_HANDLE_ENV);
// Notify ODBC that this is an ODBC 3.0 app.
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER) SQL_OV_ODBC3, 0);
CHECK_ERROR(retcode, "SQLSetEnvAttr(SQL_HANDLE_ENV)",
henv, SQL_HANDLE_ENV);
// Allocate ODBC connection handle and connect.
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_DBC)",
hdbc, SQL_HANDLE_DBC);
// Connect to DATASOURCE
retcode=SQLDriverConnect(hdbc, NULL, "DSN=DATASOURCE;", SQL_NTS,
NULL, 0, NULL, SQL_DRIVER_COMPLETE);
CHECK_ERROR(retcode, "SQLDriverConnect(DATASOURCE)",
hdbc, SQL_HANDLE_DBC);
printf ("\nConnection Attributes : \n");
// Get (some) connection attributes
// SQLUINTEGER attributes
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_ACCESS_MODE,
(SQLPOINTER) &uIntVal,
(SQLINTEGER) sizeof(uIntVal),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_ACCESS_MODE)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_ACCESS_MODE : ");
if (uIntVal==SQL_MODE_READ_ONLY) printf ("SQL_MODE_READ_ONLY");
if (uIntVal==SQL_MODE_READ_WRITE) printf ("SQL_MODE_READ_WRITE");
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_AUTO_IPD,
(SQLPOINTER) &uIntVal,
(SQLINTEGER) sizeof(uIntVal),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_AUTO_IPD)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_AUTO_IPD : ");
if (uIntVal==SQL_TRUE) printf ("SQL_TRUE");
if (uIntVal==SQL_FALSE) printf ("SQL_FALSE");
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_AUTOCOMMIT,
(SQLPOINTER) &uIntVal,
(SQLINTEGER) sizeof(uIntVal),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_AUTOCOMMIT)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_AUTOCOMMIT : ");
if (uIntVal==SQL_AUTOCOMMIT_OFF) printf ("SQL_AUTOCOMMIT_OFF");
if (uIntVal==SQL_AUTOCOMMIT_ON) printf ("SQL_AUTOCOMMIT_ON");
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_CONNECTION_DEAD,
(SQLPOINTER) &uIntVal,
(SQLINTEGER) sizeof(uIntVal),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_CONNECTION_DEAD)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_CONNECTION_DEAD : ");
if (uIntVal==SQL_CD_TRUE) printf ("SQL_CD_TRUE");
if (uIntVal==SQL_CD_FALSE) printf ("SQL_CD_FALSE");
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_CONNECTION_TIMEOUT,
(SQLPOINTER) &uIntVal,
(SQLINTEGER) sizeof(uIntVal),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_CONNECTION_TIMEOUT)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_CONNECTION_TIMEOUT : %i", uIntVal);
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_LOGIN_TIMEOUT,
(SQLPOINTER) &uIntVal,
(SQLINTEGER) sizeof(uIntVal),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_LOGIN_TIMEOUT)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_LOGIN_TIMEOUT : %i", uIntVal);
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_METADATA_ID,
(SQLPOINTER) &uIntVal,
(SQLINTEGER) sizeof(uIntVal),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_METADATA_ID)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_METADATA_ID : ");
if (uIntVal==SQL_TRUE) printf ("SQL_TRUE");
if (uIntVal==SQL_FALSE) printf ("SQL_FALSE");
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_PACKET_SIZE,
(SQLPOINTER) &uIntVal,
(SQLINTEGER) sizeof(uIntVal),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_PACKET_SIZE)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_PACKET_SIZE : %i", uIntVal);
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_TRACE,
(SQLPOINTER) &uIntVal,
(SQLINTEGER) sizeof(uIntVal),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_TRACE)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_TRACE : ");
if (uIntVal==SQL_OPT_TRACE_ON) printf ("SQL_OPT_TRACE_ON");
if (uIntVal==SQL_OPT_TRACE_OFF) printf ("SQL_OPT_TRACE_OFF");
// SQLCHAR attributes
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_CURRENT_CATALOG,
(SQLPOINTER) cStr,
(SQLINTEGER) sizeof(cStr),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_CURRENT_CATALOG)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_CURRENT_CATALOG : %s", cStr);
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_TRACEFILE,
(SQLPOINTER) cStr,
(SQLINTEGER) sizeof(cStr),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_TRACEFILE)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_TRACEFILE : %s", cStr);
retcode = SQLGetConnectAttr (hdbc,
SQL_ATTR_TRANSLATE_LIB,
(SQLPOINTER) cStr,
(SQLINTEGER) sizeof(cStr),
NULL);
CHECK_ERROR(retcode, "SQLGetConnectAttr(SQL_ATTR_TRANSLATE_LIB)",
hdbc, SQL_HANDLE_DBC);
printf ("\nSQL_ATTR_TRANSLATE_LIB : %s\n", cStr);
// Other attributes available
// SQL_ATTR_ASYNC_DBC_EVENT
// SQL_ATTR_ASYNC_DBC_FUNCTIONS_ENABLE
// SQL_ATTR_ASYNC_DBC_PCALLBACK
// SQL_ATTR_ASYNC_DBC_PCONTEXT
// SQL_ATTR_ASYNC_ENABLE
// SQL_ATTR_DBC_INFO_TOKEN
// SQL_ATTR_ENLIST_IN_DTC
// SQL_ATTR_ODBC_CURSORS
// SQL_ATTR_QUIET_MODE
// SQL_ATTR_TRANSLATE_OPTION
// SQL_ATTR_TXN_ISOLATION
exit:
printf ("\nComplete.\n");
// 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