?
Solved

How and I connect to a .MDB file without ODBC?

Posted on 2000-03-03
8
Medium Priority
?
338 Views
Last Modified: 2013-11-20
I need to wite an MFC dialog based program that can connect to a MS access .MDB file.  The problem is, that I cannot use ODBC - this program will be sent out to many clients who are complete computer novices so I can't get them to set up ODBC data sources.

Does anyone know how I can get around this problem?

- Is DAO the answer? I've heard of it, but don't know anything more.

Any help or pointers would be brilliant.

Thanks for any comments,

TimB
0
Comment
Question by:TimB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 2579485
Hi TimB,

Just a hint: you could use ODBC and write a small programm which setups the data sources programmatically when installing your software...

ZOPPO
0
 
LVL 1

Author Comment

by:TimB
ID: 2579496
Thanks  Zoppo,

I've found some info here:

http://www.codeguru.com/mfc_database/DAO_non_accessdb.shtml

And it lookslike I may be able to use the CDaoDatabase class.

I'll keep looking,  Thanks again,

TimB
0
 

Accepted Solution

by:
balugaa earned 300 total points
ID: 2579499
There is one thing you havent mentioned, is the schema of the database known, If it is pre-defined schema then DAO is the answer.

for example:

1. In your dialog class declare a CString variable:

CString m_strSourceDatabase;

2. in the dlg class constructor intialise it :

CString m_strSourceDatabase = _T("");

3. map this variable to possible an edit control: specifying the path of the database or set it your self

e.g m_strDestDatabase = "C:\\anywhere\\something.mdb";

4. Presumably you want a DB manager Class that does all the nifty database stuff.

where you declare:
CDaoRecordset* m_pSet;
CDaoDatabase m_pDatabase;

in the constructor of the database class use:
CDBMarket::CDBMarket(CString path)
{
      m_pSet = NULL;
      
      
      try
      {
            m_pDatabase.Open(path,FALSE,FALSE);
      }
      catch(CDaoException* e)
      {
            
            AfxMessageBox("Error Occurred Openeing Database");
            e->Delete();
            return;
      }
//      AfxMessageBox(m_pDatabase.GetName());
}

5. To Open a record Set:
void CDBMarket::OpenRecordSet(CString strTable)
{
      CString strQuery = _T("");
      strQuery.Format("select * from [%s]",strTable);
//      AfxMessageBox(strQuery);
      
      // initialise recordset
      try
      {
            m_pSet = new CDaoRecordset(&m_pDatabase);
      }
      catch(CDaoException* e)
      {
            AfxMessageBox("Error opening RecordSet");
            m_bConnected = false;
            e->Delete();
            return;
      }
      
      // Open Record Set
      try
      {      
      m_pSet->Open(AFX_DAO_USE_DEFAULT_TYPE, strQuery);
    }
    catch (CDaoException* e)
      {
      m_bConnected = FALSE;
      e->Delete();
      return;
    }



5. To add/ query a specific table you can use for example:

something like: from the dlg class

void CMergeDlg::AddNewRecord(CDBMarket *dbSource, CDBMarket *dbDest)
{
      int a = dbSource->m_pSet->GetFieldCount();
      int b = dbDest->m_pSet->GetFieldCount();
      CString test = _T("");
      test.Format(" Source %d, Destination %d",a,b);
      if( a != b) AfxMessageBox("Merging Different Tables");
//      AfxMessageBox(test);
      COleVariant var;
      try{
            
            if(!dbDest->AddNewRec()) AfxMessageBox("Failed again");
            
            for(int i = 0; i < dbSource->m_pSet->GetFieldCount(); i++)
            {
                  if(i != 0)
                  {
                  var = dbSource->m_pSet->GetFieldValue(i);
                  dbDest->m_pSet->SetFieldValue(i, var);
                  }
            }
            dbDest->m_pSet->Update();
      }
      catch(CDaoException* e)
      {
      //      AfxMessageBox("Error in AddNewRecord");
            e->Delete();
      }

}


Thats pretty much it.
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

Expert Comment

by:balugaa
ID: 2579559
balugaa changed the proposed answer to a comment
0
 

Expert Comment

by:balugaa
ID: 2579564
Sorry didnt want to lock the Question
0
 
LVL 1

Author Comment

by:TimB
ID: 2579603
Thats ok baluga I didn't mind you answering.  I'm just going thru your code right now.....

Tim
0
 
LVL 4

Expert Comment

by:inpras
ID: 2580461
Hi

U can configure ODBC programmatically we have developed apps that use odbc and CDatabase, which is released short time a go and up to now we havenot faced any problems with it  all that U have to do is

1. Have a tamplate to copy it  when ever user asks to create new database.

2. use SQLconfigDatasource(..) to programmatically configure the data source of ODBC (U have to supply drivers with installations).

3. Open database ( U can write an app with or without database support for this.

Hope this helps
0
 
LVL 1

Author Comment

by:TimB
ID: 2600898
Thanks balugaa!

I'd figured out some of this myself, but your gude helped loads.

Thanks v. much

TimB
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question