Advertisement
Pydzik45

[PPS]ActiveX Data Objects

May 26th, 2022
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. // ActiveXDataObjects_Pyda.cpp
  2. // Przemysław Pyda
  3. // 127361
  4. // 26.05.2022r.
  5.  
  6. #import <C:\\Program Files\\Common Files\\System\\ado\\msado15.dll> rename ("EOF", "AdoNSEOF")
  7.  
  8. #include <iostream>
  9. #include <tchar.h>
  10. using namespace std;
  11.  
  12. _bstr_t bstrConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\STUDIA\\Podstawy Programowania Sieciowego\\Lab10_ADO\\Northwind_en.accdb;";
  13.  
  14. int _tmain(int argc, _TCHAR* argv[])
  15. {
  16.     system("chcp 1250");
  17.     system("cls");
  18.  
  19.     HRESULT hr = CoInitialize(NULL); // inicjowanie obiektu COM
  20.     if (FAILED(hr))
  21.     {
  22.         cout << "Błąd wywołania funkcji CoInitialize()" << endl;
  23.         return hr;
  24.     }
  25.     try // obsługa błędów COM w bloku try catch
  26.     {
  27.         ADODB::_ConnectionPtr pConn("ADODB.Connection");
  28.         hr = pConn->Open(bstrConnect, "admin", "", ADODB::adConnectUnspecified);
  29.         if (SUCCEEDED(hr))
  30.         {
  31.             cout << "Połącznie z bazą: \n " << pConn->GetConnectionString() << endl;
  32.             _bstr_t query = // kwerenda SQL
  33.                 "SELECT Customers.[Company], Customers.[First Name] FROM Customers;";
  34.             // cout << "SQL query:\n " << query << endl;
  35.             // wysłanie kwerendy, utworzenie record set’a
  36.             ADODB::_RecordsetPtr pRS("ADODB.Recordset");
  37.             hr = pRS->Open(query,
  38.                 _variant_t((IDispatch*)pConn, true),
  39.                 ADODB::adOpenUnspecified,
  40.                 ADODB::adLockUnspecified,
  41.                 ADODB::adCmdText);
  42.             if (SUCCEEDED(hr))
  43.             {
  44.                 cout <<endl<< "Informacja o strukturze: " << endl;
  45.                 ADODB::Fields* pFields = NULL;
  46.                 hr = pRS->get_Fields(&pFields);
  47.                 if (SUCCEEDED(hr) && pFields && pFields->GetCount() > 0)
  48.                 {
  49.                     for (long nIndex = 0; nIndex < pFields->GetCount(); nIndex++)
  50.                     {
  51.                         cout << " | " << _bstr_t(pFields->GetItem(nIndex)->GetName());
  52.                     }
  53.                     cout << endl;
  54.                 }
  55.                 else
  56.                 {
  57.                     cout << "Błąd, liczba pól ma wartość zero." << endl;
  58.                 }
  59.                 cout <<endl<<endl<< "Pobieranie danych: " << endl;
  60.                 int rowCount = 0;
  61.                 while (!pRS->AdoNSEOF)
  62.                 {
  63.                     for (long nIndex = 0; nIndex < pFields->GetCount(); nIndex++)
  64.                     {
  65.                         cout << " | " << _bstr_t(pFields->GetItem(nIndex)->GetValue());
  66.                     }
  67.                     cout << endl;
  68.                     pRS->MoveNext();
  69.                     rowCount++;
  70.                 }
  71.                 cout << "Liczba pobranych wierszy: " << rowCount << endl;
  72.             }
  73.             pRS->Close();
  74.             pConn->Close();
  75.             cout << "Połączenie zamknięte" << endl;
  76.         }
  77.         else
  78.         {
  79.             cout << "Błąd połączenia z bazą: " << bstrConnect << endl;
  80.         }
  81.     }
  82.     catch (_com_error& e)
  83.     {
  84.         cout << "Błąd COM: " << e.Description() << endl;
  85.     }
  86.     CoUninitialize(); // zwolnienie biblioteki COM
  87.     return hr;
  88. }
  89.  
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement