Connecting to an Access 2000 .mdb database with visual C++ ADO commands

by CFDFEAGURU
Tags: 2000, access, commands, connecting, database, visual
 PF Gold P: 723 Hello all, I am trying to connect to an Access database, the Northwind sample database, by following this example, http://msdn.microsoft.com/en-us/library/cc811599.aspx I am using a Windows XP OS with Visual C++ 2008 Express for the compiler and IDE. The program is a console application. This example is specified for Access 2007 .accdb file types and doesn't specify if a .mdb file type will cause errors. Once I get it running correctly I will switch the path name, queries, and table names to my database. The code compiles correctly, but fails to execute. Here is the code #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #import 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; int main() { ::CoInitialize(NULL); const char* DAM = "ADO"; ADODB::_ConnectionPtr pConn("ADODB.Connection"); hr = pConn->Open(bstrConnect, "admin", "", ADODB::adConnectUnspecified); if(SUCCEEDED(hr)) { cout<GetConnectionString()<Open(query, _variant_t((IDispatch *) pConn, true), ADODB::adOpenUnspecified, ADODB::adLockUnspecified, ADODB::adCmdText); if(SUCCEEDED(hr)) { cout<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<AdoNSEOF) { for(long nIndex=0; nIndex < pFields->GetCount(); nIndex++) { cout<<" | "<<_bstr_t(pFields->GetItem(nIndex)->GetValue()); } cout<< endl; pRS->MoveNext(); rowCount++; } cout<Close(); pConn->Close(); cout<
 Mentor P: 20,433 What's the exception that is thrown? That will give you a clue as to what the problem is. Do you know how to use the Visual Studio debugger? The value stored in hr after Open returns will give you an idea as to what's the problem.
 PF Gold P: 723 The exception that is thrown is; Unhandled exception at memory location ... The same error you would get if tried to access information outside the boundaries of an array. Yes, I know how to use the debugger. The value in the Open statement says IUknown in the location for the machine name. I have tried numerous names other than "admin" and still get the same error. Thanks Matt
P: 43

Connecting to an Access 2000 .mdb database with visual C++ ADO commands

Are you sure that:

 PF Gold P: 723 silverfrost: Thanks. I had added your code but now I have a new error. The error is; :error C2660: 'ADODB::Connection15::Open' : function does not take 5 arguments So I am trying to pass too many arguments. Here is the code block where that occurs. ADODB::_RecordsetPtr pRS("ADODB.Recordset"); hr = _DC->Open(query, _variant_t((IDispatch *) _DC, true), ADODB::adOpenUnspecified, ADODB::adLockUnspecified, ADODB::adCmdText); The adCmdText is the line that the debugger points to. If I change the _DC->Open line to _DC->Open(query,"admin","",ADODB::adOpenUnspecified" The code compiles but, I get this error Unhandled exception at 0x7c812afb in Database Connection 2.exe: Microsoft C++ exception: _com_error at memory location 0x0012fbcc.. The debugger directs me to this snippet of code located in the msado15.tli file. inline HRESULT Connection15::Open ( _bstr_t ConnectionString, _bstr_t UserID, _bstr_t Password, long Options ) { HRESULT _hr = raw_Open(ConnectionString, UserID, Password, Options); if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this)); return _hr; } Any help would be greatly appreciated. Thanks Matt