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

CDatabaseExt - A Simple Way to Connect to a Database and Retrieve Data

By , 4 May 2012
Rate this:
Please Sign up or sign in to vote.
Sample Image

Introduction

This class is designed to connect to a database through ODBC and perform basic operations such as inserts, updates, deletes (and so on), and data is retrieved into a string array or into a CDBVariant array. Also, this class could serve like a CDatabase pointer to your own derived (or not) CRecordset class.

The connection string for this control could be provided through one of the constructors or through a specific method: CDatabaseExt::InitConnectionString(...). Also, you can set up a Registry folder from where the control can automatically construct the connection string. In this case, all Registry keys have the 'DS' prefix: e.g., DSName, DSServer, DSDatabase, etc. For more details, see the demo project on the CDatasourceDlg class code. This control can function connected to the database throughout or not (meaning that after every execution, the task is disconnected) and this feature is available through the SetConnectionMode(BOOL bConnectedAllTime = TRUE) method. By default, the control stays connected all the time.

The three constructors are: CDataBaseExt() which gets the connection string from the default Registry entry and the default recordset type (CRecordset::dynaset); CDatabaseExt(BOOL bConnectMode, LPCTSTR lpszRegistryEntry = NULL) where you can say what type of connection you need (connected all time or not) and your Registry entry where you have information about the connection string and recordset type; CDatabaseExt(CString sConnectionString, UINT nRsType = CRecordset::dynaset, BOOL bConnectMode = TRUE) where you can provide the connection string, recordset type, and connection mode. Of course, all this stuff can be set up later, after the class construction (connection mode, connection string, recordset type, or Registry entry).

Using the Code

In order to use this control, you have to add to your project four files: DatabaseExt.h, DatabaseExt.cpp, Crypt.h, and Crypt.cpp, the last two of them are used to encrypt the user password in the Registry. Then you simply #include "DatabaseExt.h" in the file where you intend to use the control. In the sample project, I declare in the CMyWinApp class a CDatabaseExt object member to have a database connection for the entire application:

//
// TestDatabase.h : main header file for the TESTDATABASE application
//
#include "DatabaseExt.h"

class CTestDatabaseApp : public CWinApp
{
public:
    CTestDatabaseApp();
    CDatabaseExt* GetDB(){return m_pDB;}

....
....
private:
    CDatabaseExt* m_pDB;
};

and initialize the control in InitInstance:

BOOL CTestDatabaseApp::InitInstance()
{
    m_pDB = new CDatabaseExt(_T("MyConnectionString"),CRecordset::snapshot);
}

int CTestDatabaseApp::ExitInstance() 
{
    // TODO: Add your specialized code here and/or call the base class

    if(m_pDB)delete m_pDB;

    return CWinApp::ExitInstance();
}

Suppose you need to get some data in your document class:

BOOL CTestDatabaseDoc::GetData(int& nCols,CStringArray& saItem)
{
    CString sSQL;
    sSQL.Format("SELECT * FROM mytable");

    BOOL bRet = theApp.GetDB()->GetRecordset(sSQL,saItem);
    nCols = theApp.GetDB()->GetFieldCount();
    theApp.m_sError = theApp.GetDB()->GetLastError();

    return bRet;
}

If you prefer CDBVariant instead of CStringArray, you will find a sample used in the CTestDatabaseDoc class:

BOOL CTestDatabaseDoc::GetData(int& nCols,CStringArray& saItem)
{
    CString sSQL;
    sSQL.Format("SELECT * FROM your_table");
    CTypedPtrArray<<CPtrArray,CDBVariant*>> arrItem;
    BOOL bRet = theApp.GetDB()->GetRecordset(sSQL,arrItem);

    while(arrItem.GetSize())
    {
        CDBVariant* pDBVariant = (CDBVariant*)arrItem.GetAt(0);
        // do whatever you want with pDBVariant, [for sample add items in saItem]
        delete pDBVariant;
        arrItem.RemoveAt(0);
    }
    nCols = theApp.GetDB()->GetFieldCount();
    theApp.m_sError = theApp.GetDB()->GetLastError();

    return bRet;
}

and saItem will contain all the data retrieved. Here you have to replace the SELECT ... statement with one of yours.

The demo project contains the CDataSourceDlg class that could setup the connection string values into the Registry. Here, through successive trials, I have a proper connection string to match your database. Here too, you have to replace the trial SELECT ... statements with one of yours ...

Also, in the CDataSourceDlg class implementation, you will find a very useful class CEditExt, which has a very interesting feature: can have IAutoComplete behaviour, can set up char/number filter, and limitation, can set up a custom color for text and background, etc.

License

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

About the Author

Flaviu2

Romania Romania
No Biography provided

Comments and Discussions

 
QuestionMaturity? Pinmember.dan.g.28-May-13 19:26 

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
Web04 | 2.8.140415.2 | Last Updated 4 May 2012
Article Copyright 2012 by Flaviu2
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid