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

  • What if you could rapidly grow your business while eliminating the complexity and multiple operating systems, administration and management tools, and security and compliance requirements? See how many good things happen when you standardize on open, flexible infrastructure solutions.

  • On-demand Event Event Date: January 28, 2015 Check out this webcast and join Jeff Sloyer, IBM Developer Evangelist and Master Inventor, for a tutorial for building cloud-based applications. Using IBM's platform as a service, Bluemix, Jeff will show you how to architect and assemble cloud-based applications built for cloud scale. Leveraging the power of microservices, developers can quickly translate monolithic applications to a cloud-based microarchitecture. This hour-long session introduces the concepts and …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date