Click here to Skip to main content
15,883,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

 
Share this answer
 
Comments
epanjohura 6-May-13 7:18am    
I allready tried this.. but it didn't help me
LaxmikantYadav 7-May-13 1:37am    
Can you post error or code, which is not working ? So we can help you.
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:
C++
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.
 
Share this answer
 
Comments
epanjohura 6-May-13 10:07am    
Thank you very much for your help. I'll try it now and accept it if it works..
I would try Googling for[^] that.
 
Share this answer
 
Comments
epanjohura 6-May-13 7:19am    
I did! I still have no idea how to do it.
CPallini 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900