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

CDaoDatabase....

This question has a stupid title but let me tell you what's on my
mind:

I inserted some Database tables into my project through making
a new class (derived from CDaoRecordset) for each table.

Then I added variables (each representing one table) in my main
code and open the tables through varname.Open. This is quite easy
and convenient. Now my problem is that I want a user to choose
the database on his computer (with a different path than on mine).
I use CFileDilaog and CDaoDatabase to open a database but
I do not know how to insert this new connection into my table
classe / variables. Anyone know how?

Thanks
0
Stapman
Asked:
Stapman
  • 3
1 Solution
 
StapmanAuthor Commented:
Edited text of question.
0
 
captainkirkCommented:
If you have built classes with member variables to handle the fields in tables via ClassWizard, the fields are bound to the member variables and are handled through ODBC - they reference the database you selected specifically. If you want to use other databases, you will have to manually select the network path, construct a CDatabase object, open it, construct a CDaoRecordset object, open it with a SQL statement, and navigate through it while manually retrieving values from the fields....
0
 
StapmanAuthor Commented:
Hm.
That's seems too to be much work. There must be an easier way.

My code snip (class declaration):

class db_table : public CDaoRecordset
{
public:
    db_table(CDaoDatabase* pDatabase =NULL);
    DECLARE_DYNAMIC(db_table)

    long      m_id1;
    long      m_id2;

    public:
    virtual CString GetDefaultDBName();
    virtual CString GetDefaultSQL();
    virtual void DoFieldExchange(CDaoFieldExchange* pFX);

#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif
};

Now the variable declaration in the header file of my main.cpp:

db_table m_db_table;

******************

Maybe  I should change the GetDefaultDBName() to a global path variable.....???
0
 
tommyqCommented:
Yes.
Read the documentation of both GetDefaultSQL and GetDefaultDbName and take a look at the following code:

CString CAgentSet::GetDefaultConnect()
{
      CString sConnectionString =
            ((CAgentApp*) AfxGetApp())->m_pParams->GetDbConnectionString();

      ASSERT (sConnectionString!=_T(""));
      
      if (sConnectionString.IsEmpty())
            sConnectionString = _T("ODBC;");
      
      return sConnectionString;
}

CString CAgentSet::GetDefaultSQL()
{
      return _T("[Agents]");
}

CAgentSet is the bounded class. In my case GetDefaultDbName() queries the owning application to get the DB name (quite ugly). GetDefaultSQL returns the name of the table. You can change it to return a dynamic table name.

Tommy
0
 
StapmanAuthor Commented:
Thanks.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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