Creating access database using DAO

/* Suppose you want to create an access database mcb.mdb with current version of access on  your machine. */

There are two steps:

1. Include dao headers in your project by using #include 

2. Create database using CDaoDatabase class:

CDaoDatabase daodb;  

CString szdbPath = "C:\\temp\\mcb.mdb";

try

{

       daodb.Create(szdbPath,dbLangGeneral);  //by default it will take the latest ver3.0

}

catch ( CException* e)

{

e->ReportError();

e->Delete();

 

 

Now, How to connect to an existing database?

Suppose you have an existing access database "c:\\temp\\mcb.krz".

CDaoDatabase mydb; 

CDaoRecordset* recSet;

CString szPath = "C:\\temp\\mcb.krz" ;

try

{

      mydb.Open(_szdbPath ,FALSE,FALSE );

}

catch(CDBException *e)

{

AfxMessageBox(e->m_strError);

e->Delete();

return FALSE;

}

recSet = new CDaoRecordSet( mydb );

recSet->Open() ; 

// Now you can use this recordset for adding, deleting, or positioning records.