The Generic recordset

The GenericRecordset is a simple wraper for DAO recordset that simplifies the use of the CDaoRecordset::GetFieldValue() function. It allows you to create a recordset based on a query, and recrieve the resulting set using the following functions:

  • GetCString()
  • GetShort()
  • GetLong()
  • GetDouble()
  • GetFloat()
  • GetDate() (this is a COleDate)
  • GetColumn() (this is a VARIANT)
  • GetOleColumn() (COleVariant) (new)

We have added the GenericRectodset to our small utility dll we use everywhere. This has simplifies many of the things we do. For example if you need a quick SUM of a column you would do this:

GenericRecordset SumSet(pSomeCDaoDatabase); 

SumSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"SELECT SUM(amnt_owed) FROM billing");
TotalOwed = SumSet.GetFloat(0);
SumSet.Close(); 

All the "get" functions allow you to use either the column number, or the column name. This sample could have just as easily been:
float nTotalOwed;
GenericRecordset SumSet(pSomeCDaoDatabase); 

SumSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"SELECT SUM(amnt_owed) as total_amnt FROM billing"); 
TotalOwed = SumSet.GetFloat("total_amnt");
SumSet.Close();

We not only use the GenericRecordset for simple queries, but we have just about eliminated the use of the "wizard" based recordsets in our applications. It is simpler for us and requires much less coding. We have recently added some more functionallity to the GenericRecordset. These new functions are:

SetKeys(CString) Tells the set which columns are the keys. The format is a coma delimited CString "key_field_name,key_field_name......" This is used internally by the operator == function.

CString KeyValue = "user_id,user_department";

MySet.SetKeys(KeyValue);

operator += Appends a record into the set. You can either send this function a comma delimited CString. (there is no need to put simgle quotes arounds the strings) or you can send it a similar GenericRecordset.

CString NewRecord = "1,5,Bob,Jones,San Diego";
MySet += NewRecord;

Or
MySet1 += MySet2; (where MySet1 and MySet2 are exactly the same columns)


operator = updates a record in the set. You can either send this function a comma delimited CString. (there is no need to put simgle quotes arouns the strings) or you can send it a similar GenericRecordset.

CString UpdateRecord = "1,5,Bob,Jones,San Diego";
MySet = UpdateRecord;

Or
MySet1 = MySet2; (where MySet1 and MySet2 are exactly the same columns)


operator == checks for the existance of a record in the set. This compairs 2 recordsets or a CString that contains the key values.

CString CheckRecord = "12435";
if(MySet == NewRecord)
AfxMessageBox("RecordExists");

Or
if(MySet1 == MySet2)
AfxMessageBox("Record Exists");

The GenericRecordset is simple. There is nothing special about it,but it performs a neat little job that is otherwise a pain in the fanny. There is an obvious lack of exception handling in the code. We handle all exceptions from our code. I would advice you to do the same. We are currently working on several control binding functions that will allow you to easily populate a listbox or edit control based on a query. You will see these down the road, or please feel free to make that modification and tell me about it.

If you find any bugs please let me know. Thanks and enjoy.
Also please feel free to write me about anything about the GenericRecordset.



Download source with example- 2 KB

Date updated: 27 Juli



Comments

  • The Generic Recordset

    Posted by Legacy on 02/11/2004 12:00am

    Originally posted by: Tran thanh Hai

    Would you tell me where can I have the CStringExtension class's header file, please?

    Reply
  • How to read/write image from recordset

    Posted by Legacy on 07/24/2002 12:00am

    Originally posted by: LKP

    I need to know how to read/write image of files to recordset

    Reply
  • Can I have the CStringExtension class.

    Posted by Legacy on 04/23/1999 12:00am

    Originally posted by: Chulhyung Yi

    Would you tell me where can I have the CStringExtension class's header file, please?

    Reply
  • CStringEx class

    Posted by Legacy on 02/23/1999 12:00am

    Originally posted by: Bob

    in the GeneriRecordset I use the CStringEx class written. This class is an extension class built around CString. It is GREAT! I thought I had mentioned that in the article, but after reading it now, I see I did not!

    I am sorry for any confusion that this may ave caused. The CStringEx class was written by our host here at codeguru and is one of the BEST CSrtring extensions I have used. I would recomend it to anyone, for any reason.

    I will update the artivle to reflect this as soon as I can.

    Bob

    Reply
  • CStringExtension who?

    Posted by Legacy on 01/08/1999 12:00am

    Originally posted by: Alex Dresko

    where is the the CStringExtension class that is used within the GenericRecordset class?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: April 22, 2014 Database professionals -- whether developers or DBAs -- can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this eSeminar with Dell Software's …

  • Live Event Date: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds