Code:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sys/types.h>
//globals
SQLHENV henv;//handle to connection environment
SQLHDBC hdbc;//handle to odbc connection
SQLHSTMT hstmt;//handle to sql statement
//finction declaration
void SQLDisconnectFunc();
int main ( void ){
//var dec
SQLRETURN rs;//ODBC API return status
SQLCHAR *SqlConnStringIn;
SQLCHAR ConnStringOut[1024];
SQLSMALLINT ConnStringLength;
SQLSMALLINT columns; /* number of columns in result-set */
char dsn[20] = "diadus";
int lenDsn, i, row = 0;
//var ini
lenDsn = strlen(dsn);
SqlConnStringIn = (SQLCHAR*)malloc((lenDsn + 1)*sizeof(SQLCHAR));
//load the driver manager and allocate
//environment handle
if ((rs = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv)) != 0){
fprintf (stderr, "SQLAllocHandle(): Connection allocation: failure.\n");
return;
}//end if ((result = SQL
if ((rs = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0)) != 0){
fprintf (stderr, "SQLAllocHandle(): setting of odbc version: failure.\n");
return;
}//end if
//allocate a connection handle with SQLAllocHandle
//and connects to the data source with SQLConnect, SQLDriverConnect, or SQLBrowseConnect
if (( rs = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc)) != 0){
fprintf (stderr, "SQLAllocHandle(): allocation of odbc connection: failure.\n");
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return;
}//end if
//conversion from char* to SQLCHAR*
for (i=0; i<lenDsn+1; ++i){ SqlConnStringIn[i]=dsn[i]; }//end for
rs = SQLDriverConnect(hdbc, NULL, SqlConnStringIn, lenDsn, ConnStringOut, sizeof(ConnStringOut),
&ConnStringLength, SQL_DRIVER_NOPROMPT);
if(rs != SQL_SUCCESS && rs != SQL_SUCCESS_WITH_INFO){
SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1];
SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1];
SQLINTEGER sqlcode;
SQLSMALLINT length;
SQLError(henv, hdbc, NULL, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length);
printf("SQLSTATE: %s\n", sqlstate);
printf("Native Error Code: %ld\n", sqlcode);
printf("%s \n", buffer);
//disconnect
//SQLDisconnectFunc();
system("PAUSE");
exit(-1);
//return;
}//end if
/* Allocate a statement handle */
SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
/* Retrieve a list of tables */
SQLTables(hstmt, NULL, 0, NULL, 0, NULL, 0, "TABLE", SQL_NTS);
/* How many columns are there */
SQLNumResultCols(hstmt, &columns);
/* Loop through the rows in the result-set */
while (SQL_SUCCEEDED(rs = SQLFetch(hstmt))) {
SQLUSMALLINT i;
printf("Row %d\n", row++);
/* Loop through the columns */
for (i = 1; i <= columns; i++) {
SQLINTEGER indicator;
char buf[512];
/* retrieve column data as a string */
rs = SQLGetData(hstmt, i, SQL_C_CHAR,
buf, sizeof(buf), &indicator);
if (SQL_SUCCEEDED(rs)) {
/* Handle null columns */
if (indicator == SQL_NULL_DATA) strcpy_s( buf, 1, " " );
printf(" Column %u : %s\n", i, buf);
}//emd if
}//end for
}//end while
//free helper variable
free(SqlConnStringIn);
system("PAUSE");
return (0);
}//end main
/**
* Disconnect ODBC connection
*/
void SQLDisconnectFunc() {
SQLRETURN rs = SQLDisconnect(hdbc);
if (rs != SQL_SUCCESS && rs != SQL_SUCCESS_WITH_INFO){
SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1];
SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1];
SQLINTEGER sqlcode;
SQLSMALLINT length;
SQLError(henv, hdbc, hstmt, sqlstate, &sqlcode, buffer, SQL_MAX_MESSAGE_LENGTH + 1, &length);
printf("SQLSTATE: %s\n", sqlstate);
printf("Native Error Code: %ld\n", sqlcode);
printf("%s \n", buffer);
}//enf if
//free handle to odbc connection and handle to connection environment
SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
SQLFreeHandle (SQL_HANDLE_ENV, henv);
}//end SQLDisconnect