Regarding MFC database

I already created an Consoled based application(consider prj1) using AppWizard. So that my project contains only prj.cpp,prj.h,StdAfx.cpp and StdAfx.h these files.
Now I wanted to connect to ACCESS DATABASE from my project and take some values from various tables in the database and assign it to some variables. Please inform me ASAP How can Connect and Open the database with my present project(it doesn't contain any recordset..). So what header files I have to include and to connect to the database.
balasundaramAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RAVIDCommented:
hi,
configure a DSN for the database u want to use say myDSN.
now add a new class from the class wizard derive it from CRecordSet at this time u will be asked to the DSN give the DSN as myDSN.

now u will be able to access whatever u want.

hope this helps u

cheers
0
carldeanCommented:
You'll need to

#include <afxdb.h>

in your stdafx.h for anything to compile!

And like RAVID says, you can use Class Wizard to do the rest.

If you are reading from multiple tables (that are not joined) then you are better off creating a CDatabase object and using that to connect to the database.  You can pass this database into the constructors for the CRecordsets that class wizard will generate for you.

If you want to read joined tables then the simplest way to do that is to create a Query in Access that does the join and then base the CRecordset on the query rather than the table.

There's lots of VC++ Help on this kind of stuff so you shouldn't have too many worries.

Good Luck, Carl.
0
V_BapatCommented:
Hi Bala
If you are using Access database it is better to use DAO classes provoded in MFC.

1. Include afxdao.h in stdafx.h.
2. To open the database:
CDaoDatabase db();
db.Open(/*access database filename*/);

3. To traverse through the table(say table1) :
CDaoRecordset rs(&db);
rs.Open(dbOpenDynaset, "SELECT * FROM table1");
while(!rs.IsEOF())
{
   ......
   rs.MoveNext();
}


Note : Don't forget to close the recordset and the database when you are done.

Feel free to ask if you have any doubt.

Vicky
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

balasundaramAuthor Commented:
Dear V_Bapat,

  I can understand ur idea? But it will be useful if u send some sample program.

my mail-id is sbala12@yahoo.com

rgds

balaji
0
balasundaramAuthor Commented:
Dear V_Bapat,

  I can understand ur idea? But it will be useful if u send some sample program.

my mail-id is sbala12@yahoo.com

rgds

balaji
0
balasundaramAuthor Commented:
Dear V_Bapat,

  I can understand ur idea? But it will be useful if u send some sample program.

my mail-id is sbala12@yahoo.com

rgds

balaji
0
V_BapatCommented:
Hi Bala
I will send you some sample in a couple of hours as I am not on the PC where the code is.
In the meanwhile, you can have a look at the MSDN sample Daotable and Daoview. You will get a clearer picture.

Vicky
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.