Click here to Skip to main content
13,550,805 members
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 23 Apr 2002

The Dynamic Database Class Based on ATL/OLE DB

, 23 Apr 2002
Rate this:
Please Sign up or sign in to vote.
A class to dynamically manipulate databases data using ATL/OLE DB technology
<!-- Download Links --> <!-- Article image -->

Sample Image - CSQLQuery.jpg


Imagine the advantages of being able to dynamically access a database from the client-side. You could dynamically query a database from client-side, or dynamically populate a drop-down select list, or dynamically update certain fields of a table just as a few possibilities. In fact, you can do this, with the help of the CSQLQuery object. This article describes the dynamic class CSQLQuery which based on ATL/OLE DB library and offer several samples.

Submitting an SQL statement and retrieving data

To submit an SQL statement, you simply instantiate a CSQLQuery object and then call the ExecuteSQL member function passing the SQL string as argument.

CSQLQuery query(GetSession());
query.ExecuteSQL("SELECT MAX(UnitPrice) FROM Products");

Retrieving data is easy like 1,2,3. For do that you just need to define variable and get the data by using overloaded operator like >>.

double dblUnitPrice = 0;
query >> dblUnitPrice;

Note: in this case you have to take care about data mapping. For example, SQL Server data like int should to map in C++ long type.

To retrieve any data to string and avoid mapping you can use operator > like here:

CString str; query > str.

In this case you don't have to take care about data mapping. All types of data will be map to the CString object automatically.

You can also use overloaded operators Like << to pass a parameters

double dblMaxPrice = 2.8;
CSQLQuery query(GetSession());
query << "SELECT ProductName,UnitPrice FROM Products WHERE UnitPrice > " 
      << dblMaxPrice;

The execution of stored procedure with parameters may looks like here:

double dblPar1 = 2.8, dblPar2 = 1.8;
CSQLQuery query(GetSession());
query << "EXECUTE sp_MyProcedure " << dblPar1 << " , " << dblPar2;

The SQL update statement will not complicated as well:

CSQLQuery query(GetSession());
query << "UPDATE Orders SET  ShipName = 'MyName' WHERE EmployeeID = 5";

An Example Using the Object

En example of list box population

In this example the function PoulateListBox is making population depends from the value of argument dblMaxPrice. The list box will populated with data having UnitPrice more than dblMaxPrice. You can extend that idea for any condition to retrieve data.

void PoulateListBox(CListBox& box,double dblMaxPrice)
  CSQLQuery query(GetSession());
  query << "SELECT ProductID,ProductName FROM Products WHERE UnitPrice > " 
        << dblMaxPrice;
    LONG lProductID = 0; CString strProductName; 
    query >> lProductID >> strProductName;
    int idx = box.AddString(strProductName);

An example of CListCtrl population

This sample shows how you can populate CListCtrl with columns and data.

void CQueryView::ShowQuery(const CString strTableName) 
  // Clear List control
  while(m_listCtrl.DeleteColumn(0)) {}

  CSQLQuery query(GetSession());
  query << " SELECT TOP 100 * FROM " << strTableName;
  // Show columns
  int cols = query.GetColumnsCount();
  for( int nCol = 0; nCol < cols; nCol++)
    CString strColName = query.GetColumnName(nCol);
  // Show data
  int nItem = 0;
    CString str; query > str;
    for( int nSubItem = 1; nSubItem < cols; nSubItem++)
      CString str; query > str;

Is not easy?

About GetSession()

The CSQLQuery object is using pointer to the CSession class. A CSession object represents a single database access session defined in ATL library. To create a new CSession for a CDataSource simply instantiate an objects:

CDataSource m_datasource;
CSession    m_session;
And create one using the following function:
bool Connect(ATL::CDataSource* pDataSource,ATL::CSession* pSession)
  CComBSTR bstrServer(m_strServerName);
  CComBSTR bstrUser(m_strLoginName);
  CComBSTR bstrPassword(m_strPassword);
  CComBSTR bstrDatabase(m_strDatabaseName);
  if (pSession && pSession->m_spOpenRowset != NULL)

  dbinit.AddProperty(DBPROP_AUTH_PASSWORD, bstrPassword);
  dbinit.AddProperty(DBPROP_AUTH_USERID, bstrUser);
  dbinit.AddProperty(DBPROP_INIT_CATALOG, bstrDatabase);
  dbinit.AddProperty(DBPROP_INIT_DATASOURCE, bstrServer);
  dbinit.AddProperty(DBPROP_INIT_LCID, (long)1049);
  dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);

  if(FAILED(pDataSource->Open(_T("SQLOLEDB.1"), &dbinit)))
    return false;
    if (pSession && pSession->Open(*pDataSource) != S_OK)
      return false;
  return true;
So, GetSession() will be like here:
CSession* GetSession()
  return &m_session;

Notes about demo

The demo project of this article was tested on SQL Server 7.0/8.0 and database Northwind. I believe, to connect to another type of database, for example to Oracle, it is enough to change Function Connect.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Eugene Khodakovsky
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralI got Good Luck! Pin
ekklesia2-Dec-08 20:34
memberekklesia2-Dec-08 20:34 
QuestionHow can i use stored procedure? Pin
jkYoo1-Oct-08 22:42
memberjkYoo1-Oct-08 22:42 
AnswerRe: How can i use stored procedure? Pin
ekklesia2-Dec-08 20:38
memberekklesia2-Dec-08 20:38 
QuestionConecting to MSAccess??? Pin
malfaro28-Apr-08 6:01
membermalfaro28-Apr-08 6:01 
AnswerRe: Conecting to MSAccess??? Pin
victorsow12-Jun-08 23:07
membervictorsow12-Jun-08 23:07 
GeneralRe: Conecting to MSAccess??? Pin
malfaro13-Jun-08 5:23
membermalfaro13-Jun-08 5:23 
GeneralWindows CE Support Pin
Frontier17-Jun-06 22:23
memberFrontier17-Jun-06 22:23 
GeneralRe: Windows CE Support Pin
James_Lu25-Feb-07 21:33
memberJames_Lu25-Feb-07 21:33 
GeneralProblem while getting the error msg! Pin
xifan12-Sep-05 22:43
memberxifan12-Sep-05 22:43 
Generalvarchar size error while selecting Pin
baijumax23-Jun-04 20:17
sussbaijumax23-Jun-04 20:17 
Generalthis cannot oracle Pin
Anonymous9-May-04 21:30
sussAnonymous9-May-04 21:30 
this cannot oracle data type number, varchar2
GeneralISequentialStream Pin
Urban Olars12-Mar-04 10:49
memberUrban Olars12-Mar-04 10:49 
GeneralRe: ISequentialStream Pin
CodeStylite8-Apr-04 21:50
memberCodeStylite8-Apr-04 21:50 
GeneralRe: ISequentialStream [modified] Pin
ekklesia18-Aug-09 8:31
memberekklesia18-Aug-09 8:31 
GeneralIt doesn't work under win9x! Pin
bigscholar27-Sep-03 21:57
memberbigscholar27-Sep-03 21:57 
GeneralPlease don't abuse operator &gt; ! Pin
Don Clugston24-Sep-03 13:57
memberDon Clugston24-Sep-03 13:57 
QuestionHow to built this class to a .dll? Pin
fox_hawk10-Jun-03 21:19
memberfox_hawk10-Jun-03 21:19 
GeneralRetrieving the root table of a column Pin
eshivam1-May-03 10:23
membereshivam1-May-03 10:23 
Generalthanks for your demo. Pin
xds20008-Apr-03 21:59
memberxds20008-Apr-03 21:59 
GeneralGood work Pin
Denis_B8-Apr-03 21:29
memberDenis_B8-Apr-03 21:29 
GeneralJust What I Needed Pin
Ed Gadziemski24-Apr-02 5:31
memberEd Gadziemski24-Apr-02 5:31 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 24 Apr 2002
Article Copyright 2002 by Eugene Khodakovsky
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid