#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <stdlib.h>
#include <sal.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
SQLHENV sql_hEnv = 0;
SQLHDBC sql_hDBC = 0;
SQLHSTMT sql_hStmt = 0;
SQLCHAR szDNS[1024] ={0};
SQLSMALLINT nSize = 0;
SQLRETURN sqlRet = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE,&sql_hEnv );
sqlRet =SQLSetEnvAttr( sql_hEnv,SQL_ATTR_ODBC_VERSION,(void*) SQL_OV_ODBC3,0 );
sqlRet =SQLAllocHandle( SQL_HANDLE_DBC,sql_hEnv,&sql_hDBC );
// if 0 - use SQLDriverConnect
// if 1 - use SQLConnect
//
#if 0
sqlRet =
SQLConnect( sql_hDBC,(SQLCHAR*)"people",SQL_NTS,(SQLCHAR*)"root",SQL_NTS,(SQLCHAR*)"0000",SQL_NTS );
#else
sqlRet =SQLDriverConnect( sql_hDBC,0,(SQLCHAR*)"DSN=people;UID=root;PWD=0000;",SQL_NTS,szDNS,1024,&nSize,SQL_DRIVER_COMPLETE );
#endif
if( SQL_SUCCEEDED( sqlRet ) )
{
cout << "Connected to database " << endl
<< "Connection Info: " << endl
<< szDNS << endl;
sqlRet =SQLAllocHandle( SQL_HANDLE_STMT,sql_hDBC,&sql_hStmt );
sqlRet =SQLExecDirect( sql_hStmt,(SQLCHAR*)"SELECT * FROM yadav.friends;",SQL_NTS );
SQLSMALLINT nCols = 0;
SQLINTEGER nRows = 0;
SQLINTEGER nIdicator = 0;
SQLCHAR buf[1024] = {0};
SQLNumResultCols( sql_hStmt, &nCols );
SQLRowCount( sql_hStmt, &nRows );
while( SQL_SUCCEEDED( sqlRet = SQLFetch( sql_hStmt ) ) )
{
cout << "Row " << endl;
for( int i=1; i <= nCols; ++i )
{
sqlRet = SQLGetData( sql_hStmt,i,SQL_C_CHAR,buf,1024,&nIdicator );
if( SQL_SUCCEEDED( sqlRet ) )
{
cout << "Column " << buf << endl;
}
}
} // while
SQLFreeHandle( SQL_HANDLE_STMT, sql_hStmt );
SQLDisconnect( sql_hDBC );
}
else
{
cout << "Failed to connect to the database" << endl;
}
SQLFreeHandle( SQL_HANDLE_DBC, sql_hDBC );
SQLFreeHandle( SQL_HANDLE_ENV, sql_hEnv );
return 0;
}
I2luY2x1ZGUgPHdpbmRvd3MuaD4gCiNpbmNsdWRlIDxzcWwuaD4KI2luY2x1ZGUgPHNxbGV4dC5oPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPGNvbmlvLmg+CiNpbmNsdWRlIDx0Y2hhci5oPgojaW5jbHVkZSA8c3RkbGliLmg+CiNpbmNsdWRlIDxzYWwuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBfdG1haW4oaW50IGFyZ2MsIF9UQ0hBUiogYXJndltdKQp7CglTUUxIRU5WIHNxbF9oRW52ID0gMDsKCVNRTEhEQkMgc3FsX2hEQkMgPSAwOwoJU1FMSFNUTVQgc3FsX2hTdG10ID0gMDsKCVNRTENIQVIgc3pETlNbMTAyNF0gPXswfTsKCVNRTFNNQUxMSU5UIG5TaXplID0gMDsKCSAgU1FMUkVUVVJOIHNxbFJldCA9CVNRTEFsbG9jSGFuZGxlKCBTUUxfSEFORExFX0VOViwJU1FMX05VTExfSEFORExFLCZzcWxfaEVudiApOwoJICBzcWxSZXQgPVNRTFNldEVudkF0dHIoIHNxbF9oRW52LFNRTF9BVFRSX09EQkNfVkVSU0lPTiwodm9pZCopIFNRTF9PVl9PREJDMywwICk7CgkgIHNxbFJldCA9U1FMQWxsb2NIYW5kbGUoIFNRTF9IQU5ETEVfREJDLHNxbF9oRW52LCZzcWxfaERCQyApOwovLyBpZiAwIC0gdXNlIFNRTERyaXZlckNvbm5lY3QKLy8gaWYgMSAtIHVzZSBTUUxDb25uZWN0Ci8vCiNpZiAwCnNxbFJldCA9ClNRTENvbm5lY3QoIHNxbF9oRE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