DAO issue with two MFC applications

DAO issue with two MFC applications

Post by Tod » Sun, 02 Mar 2003 01:57:41



Hello,

My problem is related to the usage of DAO with MFC.  I have two
applications running concurrently on the same CPU.  They both need to
read the same Access (.mdb) file.  At times, they need to read it
simultaneously.  What I have found, however, is that reading the .mdb
file will create some kind of lock file (.ldb) which will prevent
other processes from reading it.  This is temporary, of course, until
the database is closed, but while it is open and another process tries
to get at it, bad stuff happens.  When this occurs, I see a message
box that tells me that the app "could not lock file".  In order to
fully illustrate my problem, I'm going to run through some of the
evolution of this code...

void CMyClass::OpenDatabase()
{
  CTable tbl;   // derived from CDaoRecordset

  try{
    tbl.Open();

    while(!tbl.IsEOF()){
      <---  reads the data ----->

      tbl.MoveNext();
    }

    tbl.Close();
  }

  catch(CDaoException* dao){
    if(tbl.IsOpen())
      tbl.Close();

    dao->ReportError();

    dao->Delete();
  }

Quote:}

The code I am working on is littered with the above approach.  This
requires that the database have the read-only attribute turned off.  I
have come up with another solution (after looking at many newsgroup
posts), which allows the database to have the read-only attribute and
seems to eliminate the .ldb file, but something about it does not seem
right...

void CMyClass::OpenDatabase()
{      
  CDaoDatabase db;                                // added this line
  CMyTable tbl(&db);        // derived from CDaoRecordset

  try{
    db.Open(tbl.GetDefaultDBName(), TRUE, TRUE); // added this line
    tbl.Open();

    while(!tbl.IsEOF()){
      <---  reads the data ----->

      tbl.MoveNext();
    }

    tbl.Close();
    db.Close();                                  // added this line
  }

  catch(CDaoException* dao){
    if(tbl.IsOpen())
      tbl.Close();

    if(db.IsOpen())                             // added this line
      db.Close();                               // added this line

    dao->ReportError();

    dao->Delete();
  }

Quote:}

This opens the database, sets the exclusive flag to TRUE, and the
read-only flag to TRUE.  This looks counterintuitive to me, but it
seems to work the best of everything I've tried.  I would proceed to
make all the necessary changes in the code, but there are so many of
them that I want some assurance that this will work.  Unless I'm
mistaken, this will eliminate the .ldb file from ever coming into
existence.  However, will this allow two applications, simultaneously
making this call on the same machine, to work without showing a "could
not lock file" message box?  Will it create new issues?  If so, what
would be the best approach?

Thanks in advance, your help is appreciated.

 
 
 

1. Runing DAO DLL in non-MFC application

I've (finally) created a small sample DLL which uses DAO objects in VC5. I
can call it from a small application written also in VC5, but when I call it
from a third party application (Toolbook 6.2 in this case), the DLL causes
exception errors.

I've had no problems calling other DLLs from toolbook in the past. I am just
wondering if there is anything extra that I need to do to get the DAO DLL to
work on non-VC applications. (The DAO SDK has been installed on the
computer, but do I need to explicitly link/call the library DLLs from within
toolbook?)

2. 17702-KY-FRANKFORT-IMS-IMS DB/DC-CICS-DB2-PROGRAMMER ANALYST

3. MFC DAO with a console application

4. Please help with Interbase tables

5. Console application and MFC DAO

6. OLE DROP AND DRAG PROBLEM AND DEPLOYMENT PROBLEM

7. need help on creating installation of MFC DAO application

8. Me have deceived

9. How can I use DAO 3.6 in a MFC application

10. Calling DAO directly from MFC DAO classes

11. mfc dao vs. dao sdk

12. MFC DAO or DAO SDK.

13. DAO code to compare two fields in two tables