5,545,925 members and growing! (18,100 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

A simple count() query class

By Paul S Ganney

A class to easily do count() queries.
SQL, VC6, C++Windows, NT4, Win2K, SQL Server, MFC, VS6, Visual Studio, DBA, Dev

Posted: 16 Aug 2004
Updated: 10 Jun 2005
Views: 18,503
Bookmarked: 4 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
8 votes for this Article.
Popularity: 2.75 Rating: 3.04 out of 5
2 votes, 25.0%
1
0 votes, 0.0%
2
3 votes, 37.5%
3
1 vote, 12.5%
4
2 votes, 25.0%
5

Introduction

This simple class uses the basic CRecordset to implement count() queries. It's not too hard to do it by issuing SQL statements, it's not necessarily that hard to catch the return value. This class just makes it easier.

Background

I wrote this as part of a much larger database application. I've re-used the code several times since, so I figured it must be of use to others, too.

Using the code

To use the code, just add the code and header files to your project. You need to edit one line in countset.cpp:

CString CCountSet::GetDefaultConnect()
{
    return _T("ODBC;DSN=WinTAS");  // put your own DSN in here

}

Then just #include countset.h in the header file you want to use it in, and set it up as per the example:

CCountSet countSet;
countSet.sSelect="some SQL statement returning a numeric value, 
    such as MAX(age)";  // delete this line to use the next one

countSet.sDistinct="field name for select(distinct <fieldname>) 
    statement";  // delete this line for select(*);

countSet.sCheckTable="table name you're counting in"; 
// this is just appended to the SQL,

// so could include JOIN statements


countSet.sCheckField="WHERE condition for SQL statement";
// delete for no WHERE clause


countSet.Open();
// never Requery() it: Close() and Open() if you want to re-use it.


// return value is in countSet.m_TOTAL;

So, there it is - declare an instance of CCountSet, set some variables, and you're away. For example, to generate the SQL statement count(max(age)) from client, you'd write:

CCountSet countSet;
CString sText;

countSet.sSelect="MAX(age)";
countSet.sCheckTable="client";
countSet.Open();
sText.Format("The oldest client is %d",countSet.m_TOTAL);
AfxMessageBox(sText);

License

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

Paul S Ganney



Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular Database articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralThanks!memberEugene Plokhov1:42 18 Oct '07  
GeneralCRecordsetmemberviva dotnet7:34 18 Oct '04  
GeneralRe: CRecordsetmemberPaul S Ganney7:49 18 Oct '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Jun 2005
Editor: Smitha Vijayan
Copyright 2004 by Paul S Ganney
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project