Microsoft Developer Network >
Forums Home
>
Visual Studio Express Editions Forums
>
Visual C++ Express Edition
>
Obtaining external data from an Access 2000 .mdb database through the use C++ ADO commands
Obtaining external data from an Access 2000 .mdb database through the use C++ ADO commands
- Hello all,
I am trying to use the method outlined in this example to obtain data from an Access 2000 .mdb database and display that data in a C++ console application.
http://msdn.microsoft.com/en-us/library/cc811599.aspx
I am using the ADO example and the Northwind Access Sample Database and I am using Visual C++ Express 2008 Edition with a Windows XP operating system
The code compiles but doesn't execute correctly. I recieve an "Unhandled Exception: _com_error at memory ..." error and the debugger directs me to the hr statement. Also, I had to add the ::CoInitialize(Null) and ::CoUnitialize commands to this example.
I am assuming that the .mdb file type isn't supported by this ADO method and that this method only supports the .accdb file type. Is that correct?
#include <fstream> #include <cmath> #include <complex> #include <iostream> #include <iomanip> #include <vector> #include <limits> #include <stdlib.h> #include <stdio.h> #include <time.h> #include <fcntl.h> #include <string.h> #include <ctype.h> #include <icrsint.h> using namespace std; #import <C:\\Program Files\\Common Files\\system\\ado\\msado15.dll> rename( "EOF", "AdoNSEOF") _bstr_t bstrConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Program Files\\Microsoft Office\\Office\\Samples\\Northwind.mdb;"; HRESULT hr; //inline void TESTHR(HRESULT hr) {if FAILED(hr) _com_issue_error(hr); } int main() { ::CoInitialize(NULL); const char* DAM = "ADO"; ADODB::_ConnectionPtr pConn("ADODB.Connection"); hr = pConn->Open(bstrConnect, "admin", "", ADODB::adConnectUnspecified); if(SUCCEEDED(hr)) { cout<<DAM<<": Successfully connected to database. Data source name:\n " <<pConn->GetConnectionString()<<endl; // Prepare SQL query _bstr_t query = "SELECT Customers.[Company], Customers.[First Name] FROM Customers;"; cout <<DAM<<": SQL query \n "<<query<<endl; // Execute ADODB::_RecordsetPtr pRS("ADODB.Recordset"); hr = pRS->Open(query, _variant_t((IDispatch *) pConn, true), ADODB::adOpenUnspecified, ADODB::adLockUnspecified, ADODB::adCmdText); if(SUCCEEDED(hr)) { cout<<DAM<<": Retrieve schema info for the given result set: "<< 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<<DAM<<": Error: Number of fields in the result is set to zero." << endl; } cout<<DAM<<": Fetch the actual data: " << 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<<DAM<<": Total Row Count: " << rowCount << endl; } pRS->Close(); pConn->Close(); cout<<DAM<<": Cleanup Done" << endl; } else { cout<<DAM<<" : Unable to connect to data source: "<<bstrConnect<<endl; } ::CoUninitialize(); return 0; }
Any help with this would be greatly appreciated.
Thanks
Matt
All Replies
- According to the documentation the office 12 runtime supports the mdb file format.
Visit my (not very good) blog at http://c2kblog.blogspot.com/

