Prikazi cijelu temu 19.02.2011 00:18
zxz Van mreze
Administrator
Registrovan od:03.02.2009
Lokacija:Tuzla


Predmet:ODBC Konekcija na mdb bazu
PreuzmiIzvorni kôd (C++):
  1. #include<windows.h>
  2. #include<iostream>
  3. #include<string>
  4. #include<sql.h>
  5. #include<sqlext.h>
  6. using namespace std;
  7. int main()
  8. {
  9.   HENV hEnv;
  10.   HDBC hDbc;
  11.   RETCODE rc;
  12.   int iOut;
  13.   char strOut[256];
  14.   char szDSN[256] = "driver={Microsoft Access Driver (*.mdb)};dbq=[c:\\db1.mdb];";
  15.  
  16.   //dsn samples:
  17.   //"driver={Microsoft Access Driver (*.mdb)};dbq=[f:\\db1.mdb];"
  18.   //"driver={SQL Server};pwd={password there};Server={server name};Database={dbname there}"
  19.   //driver names for different databases:
  20.   //{SQL Server}
  21.   //{Microsoft ODBC for Oracle}
  22.   //{Oracle in oracle9}
  23.   //{Microsoft Access Driver (*.mdb)}
  24.   //{MySQL ODBC 3.51 Driver}
  25.  
  26.  
  27.   char* szSql = "select * from table1";
  28.   rc = SQLAllocEnv(&hEnv);
  29.   rc = SQLAllocConnect(hEnv, &hDbc);
  30.  
  31.   rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN,
  32.       SQL_NTS, (unsigned char*)strOut,
  33.       255, (SQLSMALLINT*)&iOut, SQL_DRIVER_NOPROMPT);
  34.   {
  35.     int ival;
  36.     char chval[128];
  37.     int ret1;
  38.     int ret2;
  39.     HSTMT hStmt;
  40.     rc = SQLAllocStmt(hDbc,&hStmt);
  41.     rc = SQLPrepare(hStmt,(unsigned char*)szSql, SQL_NTS);//1
  42.     //rc = SQLBindCol(hStmt, tab_column, tr_type, tr_value, tr_len, len_or_ind);
  43.     rc = SQLBindCol(hStmt, 1, SQL_C_ULONG, &ival, 4, (SQLINTEGER*)& ret1);
  44.     rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval, 128, (SQLINTEGER*)&ret2);
  45.     rc = SQLExecute(hStmt); //2
  46.    
  47.     //if you have queries like drop/create/many update...
  48.     //instead of //1 //2 and //3 you could use
  49.     //rc=SQLExecDirectA(hStmt,(unsigned char*)szSql,SQL_NTS);
  50.    
  51.     cout<< ">table:"<< endl;
  52.     while(1) //3
  53.     {
  54.       rc = SQLFetch(hStmt);
  55.       if(rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)break;
  56.       cout<< "{"<< ival<<"}{"<< chval<< "}"<< endl;
  57.     }
  58.     rc=SQLFreeStmt(hStmt, SQL_DROP);
  59.   }
  60.   rc = SQLDisconnect(hDbc);
  61.   rc = SQLFreeEnv(hEnv);
  62.   return 0;
  63. }

Podrška samo putem foruma, jer samo tako i ostali imaju koristi od toga.