Advertisement
paulogp

Ligação ODBC

Jul 13th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. /* Permite a ligação ODBC a uma BD */
  2.  
  3. // Apple Xcode
  4. // gcc -lodbc main.c -o teste
  5.  
  6.  
  7. #include <stdio.h>
  8. #include <sqlext.h>
  9.  
  10. int main (int argc, const char * argv[])
  11. {
  12.     // environment handle
  13.     HENV the_henv = NULL;
  14.     // connection handle
  15.     HDBC the_hdbc = NULL;
  16.     // statement handle
  17.     HSTMT the_hstmt = NULL;
  18.  
  19.     // odbc
  20.     UCHAR the_dsn[SQL_MAX_DSN_LENGTH] = "ODBC_CONNECTION_NAME";
  21.     UCHAR* the_uid = NULL; // (unsigned char *) "login";
  22.     UCHAR* the_password = NULL; // (unsigned char *) "password";
  23.  
  24.     // field var
  25.     SDWORD the_cell_buffer; // buffer
  26.     UCHAR the_column_a[255];
  27.     UCHAR the_column_b[255];
  28.     UCHAR the_column_c[255];
  29.  
  30.     // sql query
  31.     UCHAR the_sql_string[] = "SELECT column_a, column_b, column_c FROM table";
  32.  
  33.     // SQLRETURN
  34.     RETCODE the_retcode;
  35.  
  36.     // allocates an environment handle and associated resources
  37.     SQLAllocEnv(&the_henv);
  38.     // allocates a connection handle and associated resources within the environment identified by the input environment handle
  39.     SQLAllocConnect(the_henv, &the_hdbc);
  40.  
  41.     // establishes connections to a driver and a data source
  42.     the_retcode = SQLConnect(the_hdbc, the_dsn, SQL_NTS, the_uid, SQL_NTS, the_password, SQL_NTS);
  43.  
  44.     // after connection...
  45.     if (the_retcode == SQL_SUCCESS || the_retcode == SQL_SUCCESS_WITH_INFO)
  46.     {
  47.         // allocates a new statement handle and associates it with the connection specified by the connection handle
  48.         the_retcode = SQLAllocStmt(the_hdbc, &the_hstmt);
  49.         // associates an SQL statement with the input statement handle provided
  50.         the_retcode = SQLPrepare(the_hstmt, the_sql_string, sizeof(the_sql_string));
  51.         // executes a statement that was successfully prepared using SQLPrepare() on the same statement handle
  52.         the_retcode = SQLExecute(the_hstmt);
  53.  
  54.         if (the_retcode == SQL_SUCCESS || the_retcode == SQL_SUCCESS_WITH_INFO)
  55.         {
  56.             while (the_retcode == SQL_SUCCESS || the_retcode == SQL_SUCCESS_WITH_INFO)
  57.             {
  58.                 // binds application data buffers to columns in the result set
  59.                 SQLBindCol(the_hstmt, 1, SQL_C_CHAR, the_column_a, sizeof(the_column_a), &the_cell_buffer);
  60.                 SQLBindCol(the_hstmt, 2, SQL_C_CHAR, the_column_b, sizeof(the_column_b), &the_cell_buffer);
  61.                 SQLBindCol(the_hstmt, 3, SQL_C_CHAR, the_column_c, sizeof(the_column_c), &the_cell_buffer);
  62.  
  63.                 the_retcode = SQLFetch(the_hstmt);
  64.                 if (the_retcode != SQL_NO_DATA_FOUND)
  65.                 {
  66.                     printf("%s %s %s\n", the_column_a, the_column_b, the_column_c);
  67.                 }
  68.                 else
  69.                 {
  70.                     printf("...");
  71.                 }
  72.             }
  73.         }
  74.         else
  75.         {
  76.             printf("erro: na construcao da consulta sql\n");
  77.         }
  78.  
  79.         // process data
  80.         if (the_retcode == SQL_SUCCESS || the_retcode == SQL_SUCCESS_WITH_INFO)
  81.         {
  82.             SQLCancel(the_hstmt);
  83.             SQLFreeHandle(SQL_HANDLE_STMT, the_hstmt);
  84.         }
  85.  
  86.         // free
  87.         SQLFreeStmt(the_hstmt, SQL_DROP);
  88.         SQLDisconnect(the_hdbc);
  89.     }
  90.     else
  91.     {
  92.         printf("erro: sem sucesso\n");
  93.     }
  94.  
  95.     // free hEnv
  96.     SQLFreeEnv(the_henv);
  97.  
  98.     // sleep windows (Sleep): ms; posix (sleep): seconds
  99.     sleep(5);
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement