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.
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:
#include countset.h in the header file you want to use it in, and set it up as per the example:
countSet.sSelect="some SQL statement returning a numeric value,
such as MAX(age)";
countSet.sDistinct="field name for select(distinct <fieldname>)
countSet.sCheckTable="table name you're counting in";
countSet.sCheckField="WHERE condition for SQL statement";
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:
sText.Format("The oldest client is %d",countSet.m_TOTAL);