Connect to Access Database with OLEDB

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Ive found a ton of info on VB (of which I can already do) but trying to get any info on C++ seems to be non-existent. I am running into a problem of intermittent problems with a SELECT INTO while using ODBC so I figured lets give OLEDB a
shot and see if there is any difference. Youd think that would be easy but after 90+ min of searcing for examples of how to connect to an Access DB with OLEDB Ive come up with basically nothing. This is my class that I have for connecting to
the database:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; class dbCalls2
{
<span style="color:Blue; public:
CDatabase cdw2;

<span style="color:Blue; int TempQueryDbaseOpenOleDb(CString *filename)
{

<span style="color:Blue; int ok = 1; <span style="color:Green; // error flag
CString localfilename = filename[0]; <span style="color:Green; // perform manipulation on filename being passed to remove .mdb if its appended
localfilename.MakeUpper();
<span style="color:Green; //if ( localfilename.Right(4) == _T(".MDB"))
<span style="color:Green; //localfilename.Delete(localfilename.GetLength()-4,4);
<span style="color:Blue; if ( localfilename.Right(6) == _T(<span style="color:#A31515; ".ACCDB"))
localfilename.Delete(localfilename.GetLength()-6,6);

CString wDSNString = _T(<span style="color:#A31515; "Provider=Microsoft.ACE.OLEDB.12.0; Data Source =") + localfilename + _T(<span style="color:#A31515; ";Uid=Admin;Pwd=;");

cdw2.SetLoginTimeout( 60 );
cdw2.SetQueryTimeout(7200);

<span style="color:Blue; try
{
cdw2.Open(NULL, FALSE, FALSE, wDSNString, FALSE);
}
<span style="color:Blue; catch(CDBException *e )
{
AfxMessageBox(_T(<span style="color:#A31515; "Didnt open OLEDB MDB database.n") + e->m_strError);
<span style="color:Blue; throw;
ok = 0;
}
<span style="color:Blue; return(ok);

} <span style="color:Green; // end TempQueryDbaseOpen
[/code]
it gets to the open and then I get a "Select Data Source" window with a message of "Select the file data source that describes the driver that you with to connect to. You can use any file date source that refers to an ODBC driver which is installed
on your machine"...
Obviously still new to C++ so any help would be appreciated. Thanks!!!

View the full article
 
Back
Top