fork download
  1. #include <windows.h>
  2. #include <sql.h>
  3. #include <sqlext.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <tchar.h>
  7. #include <stdlib.h>
  8. #include <sal.h>
  9.  
  10. using namespace std;
  11. int _tmain(int argc, _TCHAR* argv[])
  12. {
  13. SQLHENV sql_hEnv = 0;
  14. SQLHDBC sql_hDBC = 0;
  15. SQLHSTMT sql_hStmt = 0;
  16. SQLCHAR szDNS[1024] ={0};
  17. SQLSMALLINT nSize = 0;
  18. SQLRETURN sqlRet = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE,&sql_hEnv );
  19. sqlRet =SQLSetEnvAttr( sql_hEnv,SQL_ATTR_ODBC_VERSION,(void*) SQL_OV_ODBC3,0 );
  20. sqlRet =SQLAllocHandle( SQL_HANDLE_DBC,sql_hEnv,&sql_hDBC );
  21. // if 0 - use SQLDriverConnect
  22. // if 1 - use SQLConnect
  23. //
  24. #if 0
  25. sqlRet =
  26. SQLConnect( sql_hDBC,(SQLCHAR*)"people",SQL_NTS,(SQLCHAR*)"root",SQL_NTS,(SQLCHAR*)"0000",SQL_NTS );
  27. #else
  28. sqlRet =SQLDriverConnect( sql_hDBC,0,(SQLCHAR*)"DSN=people;UID=root;PWD=0000;",SQL_NTS,szDNS,1024,&nSize,SQL_DRIVER_COMPLETE );
  29. #endif
  30. if( SQL_SUCCEEDED( sqlRet ) )
  31. {
  32. cout << "Connected to database " << endl
  33. << "Connection Info: " << endl
  34. << szDNS << endl;
  35. sqlRet =SQLAllocHandle( SQL_HANDLE_STMT,sql_hDBC,&sql_hStmt );
  36. sqlRet =SQLExecDirect( sql_hStmt,(SQLCHAR*)"SELECT * FROM yadav.friends;",SQL_NTS );
  37. SQLSMALLINT nCols = 0;
  38. SQLINTEGER nRows = 0;
  39. SQLINTEGER nIdicator = 0;
  40. SQLCHAR buf[1024] = {0};
  41. SQLNumResultCols( sql_hStmt, &nCols );
  42. SQLRowCount( sql_hStmt, &nRows );
  43. while( SQL_SUCCEEDED( sqlRet = SQLFetch( sql_hStmt ) ) )
  44. {
  45. cout << "Row " << endl;
  46. for( int i=1; i <= nCols; ++i )
  47. {
  48. sqlRet = SQLGetData( sql_hStmt,i,SQL_C_CHAR,buf,1024,&nIdicator );
  49. if( SQL_SUCCEEDED( sqlRet ) )
  50. {
  51. cout << "Column " << buf << endl;
  52. }
  53. }
  54. } // while
  55. SQLFreeHandle( SQL_HANDLE_STMT, sql_hStmt );
  56. SQLDisconnect( sql_hDBC );
  57. }
  58. else
  59. {
  60. cout << "Failed to connect to the database" << endl;
  61. }
  62. SQLFreeHandle( SQL_HANDLE_DBC, sql_hDBC );
  63. SQLFreeHandle( SQL_HANDLE_ENV, sql_hEnv );
  64. return 0;
  65. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty