Connecting two database


  I know that we can connect two database in an aplication.  I'm using Access Database and ODBC MFC Database classes/CRecordset to access the database.  In my aplication, I want to retrieve data from two different database in a single document or dialog box, is that possible?

  I don't know where I should create the multiple CDatabase.

  I need a step by step instructions how to make the program work.

  Thank you.
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.

Since you are using Access database, I would first suggest that you use DAO classes. MS recommends that you use this.
But if your requirement is that you have to use ODBC, it is no problem.

What you want *can* be done. If you give some more info about your app, it would be more useful for giving better suggestions.

Anyway, what you should to do is:
1. Derive 2 classes from CRecordset for the two databases.
2. Construct 2 CDatabase objects.
3. Open the databases.
4. Construct the objects for the recordset and open them.

You are ready now. For example:

Suppose you have derived CSet1 and CSet2 from CRecordset.

CDatabase* db1, db2;

CSet1 rsSet1(db1);
CSet2 rsSet2(db2);


/* Read the records*/

/* Read the records*/



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
hamidahAuthor Commented:
 Hi, thank you for your answer.  Let me tell you a little more about the application.  I have a dialog box.  In the dialog box there are four edit box, two box will retrieve from one database and two others will retrieve data from another database.  I use MFC AppWizard(exe) to start my project and then add two new class derived from recordset for the two databases.  What happened now is, when i run the application there is a "debug assertion failed' error.  Or if I use a single document with database view, only two edit box will display data from one database.

  What is wrong?  Will you help me?

  Thank you.
Debug to find out what is causing the assertion failure. Only then you can find out the reason for it.

If you are displaying the records in the dialog box, then you declare the database and recordset as member of your dialog class. You should open the database, read records and set the edit box text in OnInitDialog().

Or you can use CRecordView. I have never used it, so I cannot be of much help to you.

All the best.

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.