Click here to Skip to main content
Click here to Skip to main content

Query Database From MFC (Database Viewer)

, 4 Dec 2013
Rate this:
Please Sign up or sign in to vote.
How to query a database file from an MFC application.

Sample Image


Today in many projects, we need a database for our application as a result of which we have to link to the database file from our application. With this tip, we can connect to a database from MFC and query or get a report from that.


To get information on this subject, you can read or search about Connect to database from application.

Algorithm and Simulating the Algorithm in Code

At first, we have to determine what database is required. With the choice of a combobox, we decide what string connection to the database is needed. If we choose Access Database, our string connection is like below:

Driver={Microsoft Access Driver (*.mdb)};Server:.;Dbq=db.mdb;Uid=;Pwd=;

and if we choose Excel Database, that's like:

Driver={Microsoft Excel Driver (*.xls)};

and if we choose SQL Database, that will be:

Driver={SQL Server Native Client 11.0};Server=.;

After that, we run a query on the database. We have to make sure that the query's type is Reporting Query or Execute Query.

The queries that get information from the database are Reporting Queries like "select * from table". And other queries that do work or affect the database are Execute Queries like "update id form table1 where name='mahdi'". To determine this algorithm, we write a function to get the query string and determine that with the below code:

bool IsReport(CString code){
    if(code.Left(6)==_T("insert")||code.Left(6)==_T("update"))return 0;
    return 1;

Now we decide to get a report from the database or send an execute query with the above function. If the IsReport() function returns 0, it means that we have to send the query as a getting report or else we have to send the query as an execute query. Statements like insert, update,.... affect the database, so these are execute queries and we send these queries in CDatabse and other statements get reported and we send these with the CRecordset class. The CDatabase class can do execute and CReordset can fields of queries. With this code, you can get do it better:

CString query;
if(IsReport(query)){//this is a report command
    CRecordset recordset(&database);
    CString temp,record;
    while(!recordset.IsEOF()){//is null
        register int len=recordset.GetODBCFieldCount();
        for(register int i=0; i < len ; i++){
                record+=temp+_T("   |   ");
MessageBox(_T("Query done."),0,0);

I hope this will be useful for you. Thanks in advance.


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

About the Author

Mahdi Nejadsahbei
Software Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

QuestionStatement(s) could not be prepared PinmemberMember 862231727-Mar-14 8:33 
AnswerRe: Statement(s) could not be prepared PinmemberMahdi Nejadsahbei6-Apr-14 13:05 
GeneralRe: Statement(s) could not be prepared PinmemberMember 86223176-Apr-14 21:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 4 Dec 2013
Article Copyright 2013 by Mahdi Nejadsahbei
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid