• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

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.
0
balasundaram
Asked:
balasundaram
1 Solution
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now