Click here to Skip to main content
12,403,130 members (70,298 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.9K views
964 downloads
11 bookmarked
Posted

Query Database From MFC (Database Viewer)

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

Sample Image

Introduction

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.

Background

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)};
DriverId=790;bq=C:\\DatabasePath\\DBSpreadSheet.xls;DefaultDir=c:\\databasepath;

and if we choose SQL Database, that will be:

Driver={SQL Server Native Client 11.0};Server=.;
AttachDbFilename=shop.mdf;Database=shop;Trusted_Connection=Yes;

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){
    code.TrimLeft();code.TrimRight();code.MakeLower();
    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;
tquery.GetWindowText(query);
list.ResetContent();
if(IsReport(query)){//this is a report command
    CRecordset recordset(&database);
    CString temp,record;
    recordset.Open(CRecordset::forwardOnly,query,CRecordset::readOnly);
    while(!recordset.IsEOF()){//is null
        record=_T("");
        register int len=recordset.GetODBCFieldCount();
        for(register int i=0; i < len ; i++){
                recordset.GetFieldValue(i,temp);
                record+=temp+_T("   |   ");
        }
        list.AddString(record);
        recordset.MoveNext();
    }
}else{
    database.ExecuteSQL(query);
}
MessageBox(_T("Query done."),0,0);

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

License

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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
Questionneed password! Pin
Member 1149549411-Jul-15 19:01
memberMember 1149549411-Jul-15 19:01 
AnswerRe: need password! Pin
Mahdi Nejadsahbei11-Jul-15 21:56
memberMahdi Nejadsahbei11-Jul-15 21:56 
GeneralRe: need password! Pin
rdw6-Mar-16 21:36
memberrdw6-Mar-16 21:36 
QuestionStatement(s) could not be prepared Pin
Member 862231727-Mar-14 8:33
memberMember 862231727-Mar-14 8:33 
AnswerRe: Statement(s) could not be prepared Pin
Mahdi Nejadsahbei6-Apr-14 13:05
memberMahdi Nejadsahbei6-Apr-14 13:05 
GeneralRe: Statement(s) could not be prepared Pin
Member 86223176-Apr-14 21:39
memberMember 86223176-Apr-14 21:39 
GeneralRe: Statement(s) could not be prepared Pin
Mahdi Nejadsahbei21-Dec-14 6:15
memberMahdi Nejadsahbei21-Dec-14 6:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 4 Dec 2013
Article Copyright 2013 by Mahdi Nejadsahbei
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid