[RESOLVED] VC++ 6 connecting to Access db via ADO problems
The company has been using DAO for the past umpteen years, but DAO won't work with multi-threaded applications.
But I needed to connect to an Access database and read some values out. I didn't need to port the entire superset of functionality across, so I decide to hack out part of the functionality. I quickly hit code guru and codeproject for simple libraries and after a quick scanning it seemed to me that Tim Kohler is MFC ADO Wrapper (http://www.codeproject.com/database/adoclasses.asp) classes were ideal.
So I quickly added in what functionality I needed and tried to connect to the database. Unfortunately I couldn't. A quick attack of his code gave me the error. The error I got was "Unspecified error". Hmm helpful. A bit of messing with the connection strings gave me an "IDispatch error #3187" error.
The interweb didn't turn up much on it so I decide to go back to basics and write one myself. Just to connect and nothing else and I got the previous two errors plus the "Unknown Error 0x800A0E7A" with various tweaking of the connection and provider.
In the end I decided it might be the database I was trying to connect to - the one I was trying to connect to had a long path name (about 40 characters and two of the paths had spaces) as well as the database was passworded but had no username.
The test database was in the root of C:, had 1 table with 2 columns and 5 rows.
Still I couldn't connect - the same several errors I had earlier with various incarnations of the connect string.
Is there an easy test to see if ADO is working on my PC, or can anyone give any reason why this doesn't work on my machine?
Thanks, Graham Reeds
The code:
HRESULT hr = ::CoInitialize(NULL);
if (FAILED(hr))
{
return false;
}
_ConnectionPtr pConnection;
hr = pConnection.CreateInstance(__uuidof(Connection));
if (FAILED(hr))
{
return false;
}
try
{
CString strProvider("Microsoft.JET.OLEDB.4.0");
m_strDatabasePath = "c:\\db1.mdb";
CString strConnection;
strConnection.Format("Provider=%s;"
"DataSource=%s;"
"Uid=%s;"
"Pwd=%s;",
strProvider,
m_strDatabasePath,
"",
"");
pConnection->Open(_bstr_t(strConnection), _bstr_t(""), _bstr_t(""), adOpenUnspecified);
// ...
}
catch (_com_error e)
{
CString strErr(e.ErrorMessage());
::CoUninitialize();
// ...
}

