Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <afxdb.h>
  2. #include <stdio.h>
  3. #include <sql.h>
  4. #include <sqltypes.h>
  5. #include <sqlext.h>
  6. #include <string>
  7. #include <iostream>
  8.  
  9. void extract_error(
  10. char *fn,
  11. SQLHANDLE handle,
  12. SQLSMALLINT type)
  13. {
  14. SQLINTEGER i = 0;
  15. SQLINTEGER native;
  16. SQLWCHAR state[7];
  17. SQLWCHAR text[256];
  18. SQLSMALLINT len;
  19. SQLRETURN ret;
  20. fprintf(stderr,
  21. "\n"
  22. "The driver reported the following diagnostics whilst running "
  23. "%s\n\n",
  24. fn);
  25.  
  26. do
  27. {
  28. ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
  29. sizeof(text), &len);
  30. if (SQL_SUCCEEDED(ret))
  31. printf("%s:%ld:%ld:%s\n", state, i, native, text);
  32. } while (ret == SQL_SUCCESS);
  33. }
  34.  
  35. void connectTest(SQLCHAR *conn)
  36. {
  37. SQLHANDLE henv;
  38. SQLRETURN rc;
  39. SQLHANDLE hconn;
  40. SQLSMALLINT bufsize = 0;
  41. SQLINTEGER nativeerror = 0;
  42. SQLSMALLINT textlen = 0;
  43. SQLCHAR connStrOut[256];
  44. SQLCHAR sqlstate[32];
  45. SQLCHAR message[256];
  46.  
  47. rc = SQLAllocEnv(&henv);
  48. if (rc != SQL_SUCCESS)
  49. {
  50. printf("\nSQLAllocEnv call failed.");
  51. return;
  52. }
  53.  
  54. rc = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hconn);
  55. if (rc != SQL_SUCCESS)
  56. {
  57. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  58. printf("\nSQLAllocHandle call failed.");
  59. return;
  60. }
  61.  
  62. rc = SQLDriverConnectA(hconn, NULL,
  63. conn, SQL_NTS,
  64. connStrOut, 256, &bufsize,
  65. SQL_DRIVER_NOPROMPT);
  66.  
  67. if (bufsize != 0)
  68. {
  69. printf("Connected successfully.\n");
  70. SQLDisconnect(hconn);
  71. }
  72. else
  73. {
  74. rc = SQLGetDiagRecA(SQL_HANDLE_DBC, hconn, 1,
  75. sqlstate, &nativeerror, message, 256, &textlen);
  76.  
  77. printf("SQLDriverConnect failed.\n");
  78. if (rc != SQL_ERROR)
  79. printf("%s=%s\n", (CHAR *)sqlstate, (CHAR *)message);
  80. }
  81.  
  82. SQLFreeHandle(SQL_HANDLE_DBC, hconn);
  83. SQLFreeHandle(SQL_HANDLE_ENV, henv);
  84. }
  85.  
  86. void main()
  87. {
  88. connect((SQLCHAR*)"Driver={PostgreSQL};Server=192.168.1.140;Port=5432;Uid = postgres; Database=postgres;Pwd=1234");
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement