Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ActiveXDataObjects_Pyda.cpp
- // Przemysław Pyda
- // 127361
- // 26.05.2022r.
- #import <C:\\Program Files\\Common Files\\System\\ado\\msado15.dll> rename ("EOF", "AdoNSEOF")
- #include <iostream>
- #include <tchar.h>
- using namespace std;
- _bstr_t bstrConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\STUDIA\\Podstawy Programowania Sieciowego\\Lab10_ADO\\Northwind_en.accdb;";
- int _tmain(int argc, _TCHAR* argv[])
- {
- system("chcp 1250");
- system("cls");
- HRESULT hr = CoInitialize(NULL); // inicjowanie obiektu COM
- if (FAILED(hr))
- {
- cout << "Błąd wywołania funkcji CoInitialize()" << endl;
- return hr;
- }
- try // obsługa błędów COM w bloku try catch
- {
- ADODB::_ConnectionPtr pConn("ADODB.Connection");
- hr = pConn->Open(bstrConnect, "admin", "", ADODB::adConnectUnspecified);
- if (SUCCEEDED(hr))
- {
- cout << "Połącznie z bazą: \n " << pConn->GetConnectionString() << endl;
- _bstr_t query = // kwerenda SQL
- "SELECT Customers.[Company], Customers.[First Name] FROM Customers;";
- // cout << "SQL query:\n " << query << endl;
- // wysłanie kwerendy, utworzenie record set’a
- ADODB::_RecordsetPtr pRS("ADODB.Recordset");
- hr = pRS->Open(query,
- _variant_t((IDispatch*)pConn, true),
- ADODB::adOpenUnspecified,
- ADODB::adLockUnspecified,
- ADODB::adCmdText);
- if (SUCCEEDED(hr))
- {
- cout <<endl<< "Informacja o strukturze: " << endl;
- ADODB::Fields* pFields = NULL;
- hr = pRS->get_Fields(&pFields);
- if (SUCCEEDED(hr) && pFields && pFields->GetCount() > 0)
- {
- for (long nIndex = 0; nIndex < pFields->GetCount(); nIndex++)
- {
- cout << " | " << _bstr_t(pFields->GetItem(nIndex)->GetName());
- }
- cout << endl;
- }
- else
- {
- cout << "Błąd, liczba pól ma wartość zero." << endl;
- }
- cout <<endl<<endl<< "Pobieranie danych: " << endl;
- int rowCount = 0;
- while (!pRS->AdoNSEOF)
- {
- for (long nIndex = 0; nIndex < pFields->GetCount(); nIndex++)
- {
- cout << " | " << _bstr_t(pFields->GetItem(nIndex)->GetValue());
- }
- cout << endl;
- pRS->MoveNext();
- rowCount++;
- }
- cout << "Liczba pobranych wierszy: " << rowCount << endl;
- }
- pRS->Close();
- pConn->Close();
- cout << "Połączenie zamknięte" << endl;
- }
- else
- {
- cout << "Błąd połączenia z bazą: " << bstrConnect << endl;
- }
- }
- catch (_com_error& e)
- {
- cout << "Błąd COM: " << e.Description() << endl;
- }
- CoUninitialize(); // zwolnienie biblioteki COM
- return hr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement