Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC
Hey Guys
 
I've a database (MS Access) and I created a MFC-Windows Application. Now I want to open the database in my tool and get some values out of it.
How do I have to do it with CDaoDatabase? I have no idea.
 
Thanks for your help.
 

Greetings
Epanjohura
Posted 6-May-13 1:30am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
epanjohura at 6-May-13 7:18am
   
I allready tried this.. but it didn't help me
LaxmikantYadav at 7-May-13 1:37am
   
Can you post error or code, which is not working ? So we can help you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There are many ways to use the CDao* classes. If you only want to read some fields from one or more tables, you can omit the creation of special derived classes to access them:
CDaoDatabase db;
try
{
    // Open access file: non-exclusive, read only
    // When db is password protected, pass "PWD=password" as 4th argument
    db.Open(lpszFileName, FALSE, TRUE);
    // Open a table
    CDaoTableDef TableDef(&db);
    TableDef.Open(lpszTableName);
    // Step through all recordsets in the table and get
    //  the value for a specific field
    CDaoRecordset rc;
    rc.Open(&TableDef);
    if (rc.IsOpen())
    {
        if (!rc.IsBOF())
        {
            while (!rc.IsEOF())
            {
                // Get the content of the field
                COleVariant var;
                rc.GetFieldValue(lpszFieldName, var);
                // print content if a string or integer 
                if (VT_BSTR == var.vt)
                {
                    CString str = V_BSTR(&var);
                    _tprintf(_T("%s = %s\n"), lpszFieldName, str.GetString());
                }
                else if (VT_I4 == var.vt)
                    _tprintf(_T("%s = %d\n"), lpszFieldName, var.lVal);
                rc.MoveNext();
            }
        }
        rc.Close();
    }
    if (TableDef.IsOpen())
        TableDef.Close();
 
    // Execute a SQL command
    CString str;
    str.Format(_T("SELECT * FROM [%s]"), lpszTableName);
    CDaoRecordset rc1(&db);
    rc1.Open(dbOpenDynaset, str.GetString());
    if (rc1.IsOpen())
    {
        // step through the recordsets like in the above code
        //  and access fields by name or index
        rc1.Close();
    }
}
catch (CDaoException * pe)
{
    // handle errors here
    pe->Delete();
}
if (db.IsOpen())
    db.Close();
 
As you can see, the main work is performed using the CDaoRecordset class. Pass a pointer to your database to the constructor when executing SQL commands with the Open() function or pass a pointer to a table or query definition when opening these. Then step through the recordsets using the Move...() functions and retrieve (or set) fields by accessing them by their name or index.
 
Before proceeding you should also read about the VARIANT type and the MFC COleVariant encapsulation.
  Permalink  
Comments
epanjohura at 6-May-13 10:07am
   
Thank you very much for your help. I'll try it now and accept it if it works..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I would try Googling for[^] that.
  Permalink  
Comments
epanjohura at 6-May-13 7:19am
   
I did! I still have no idea how to do it.
CPallini at 6-May-13 7:57am
   
There are many many (did I write 'many'?) examples available, cannot you modify one of them in order to fit your needs?
epanjohura at 6-May-13 7:58am
   
I'm trying.. but until now I didn't understand anything..!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 275
1 OriginalGriff 230
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 6 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100