Click here to Skip to main content
15,867,686 members
Articles / Web Development / HTML

CODBCRecordset Class

Rate me:
Please Sign up or sign in to vote.
4.91/5 (37 votes)
6 Jan 2001CPOL 449K   3.8K   102   150
CODBCRecordset class is intended to be a full replacement of all ClassWizard generated CRecordset derived classes in MFC projects.

Table of Contents

Introduction

In a usual MFC database project, we have a lot of CRecordset derived classes generated by ClassWizard. The class presented here, CODBCRecordset is a very easy to use replacement of all these CRecordset classes. CODBCRecordset is derived from CRecordset but it does not have hardcoded inside the number and the type of the database fields.

I have seen some implementations trying to solve this problem. But CODBCRecordset has at least two advantages:

  1. CODBCRecordset can be used not only to get values from the database, but to write values into the database using the MFC way.
  2. The other implementations cannot simultaneously open more than one recordset through one database connection when the database is MS SQL Server. In some cases, this could be a very big problem because opening a new connection is expensive in terms of time and resources.

Because CODBCRecordset is derived from CRecordset and uses its data exchange mechanism, it is fully compatible with MFC ODBC class CDatabase .

Each field value is stored in a CDBField object. The CDBField class is inherited from CDBVariant and has some added functionality so it's simple to use.

CODBCRecordset and CDBField classes support all database data types. CDBField makes implicit type conversion where it is appropriate to supply the data in the requested format.

Here is a list of CODBCRecordset and CDBField methods:

CODBCRecordset class
Constructor The same as for CRecordset accepting CDatabase*
BOOL Open( LPCTSTR lpszSQL, UINT nOpenType, DWORD dwOptions ); Open the recordset
lpszSQL is a SQL statement that returns recordset
e.g. SELECT * FROM tablename
nOpenType is CRecordset open type, see CRecordset::Open()dwOptions is CRecordset options, see CRecordset::Open()

Note that lpszSQL and nOpenType have exchanged their positions compared to CRecordset::Open()

short GetODBCFieldCount() Returns number of the fields (columns) in the recordset. This method is defined in CRecordset .
int GetFieldID( LPCTSTR ) Returns a field index by given field name. It is case insensitive. CRecordset::GetFieldIndexByName() works, but is case sensitive.
CString GetFieldName( int ) Returns a field name by given field index
CDBField& Field( LPCTSTR )<br />
			CDBField& Field( int )
Through this method, you can get a reference to the internal CDBField object corresponding to the specified column (See the CDBField class table for details about CDBField ).

There are two forms of the method - with argument of type LPCTSTR szName - specifying the name of the column, and int nID - specifying the index of the coulmn in the recordset.

These methods can be used as lvalue in expressions. Thus, you can write values to the database.

CDBField& operator( LPCTSTR )<br />
			CDBField& operator( int )
The function operator is defined for ease of use of the class and just calls the corresponding Field() method

There are two forms of the function operator - with argument of type LPCTSTR szName - specifying the name of the column, and int nID - specifying the index of the column in the recordset.

These can be used as lvalue in expressions. Thus you can write values to the database.

bool GetBool()
unsigned char GetChar()
short GetShort()
int GetInt()
long GetLong()
float Getfloat()
double GetDouble()
COleDateTime GetDate()
CString GetString()
CLongBinary* GetBinary()
All of these methods do the appropriate type conversions depending on their return value and the type of the underlying data in the database.

These methods just call Field().AsXXX()
(See the CDBField class table for details about CDBField).

There are two forms of these methods - with argument of type LPCTSTR szName - specifying the name of the column, and int nID - specifying the index of the coulmn in the recordset.

These cannot be used as lvalue in expressions.

CDBField class
Constructors No public constructors. CDBField cannot be instantiated except by CODBCRecordset to be used in internal structures to support data exchange. These objects are accessible through CODBCRecordset methods.
bool AsBool()
unsigned char AsChar()
short AsShort()
int AsInt()
long AsLong()
float AsFloat()
double AsDoble()
COleDateTime AsDate()
CString AsString()
CLongBinary* AsBinary()
All of these methods do the appropriate type conversions depending on their return value and the type of the underlying data in the database (See the AsXXX methods table for data conversion rules).

There is no data type Int but AsInt() is equal to AsLong()

assignment operators There are defined assignment operators accepting bool, unsigned char, short, int, long, COleDateTime and CString. So CDBField objects can be lvalues. There is no assignment operator accepting CLongBinary because MFC does not support writing CLongBinary values into database. These assignment operators do appropriate conversions to the underlying database column data type except CLongBinary (See the assignment operators table for data conversion rules).
const CString& GetName() Returns the field name this object corresponds to.
bool IsNull()
bool IsBool()
bool IsChar()
bool IsShort()
bool IsInt()
bool IsLong()
bool IsFloat()
bool IsDouble()
bool IsNumber()
bool IsDate()
bool IsString()
bool IsBinary()
Each of these return true if the field contains a value of the corresponding data type.

There is no data type Number but IsNumber() returns true if IsShort() || IsLong() || IsFloat() || IsDouble().

There is no data type Int but IsInt() returns true if IsLong() returns true.

Conversions made by AsXXX methods

 

Values in the database

  NULL BOOL UCHAR SHORT LONG SINGLE DOUBLE DATE STRING BINARY
AsBool false * * * * * *   *  
AsChar 0x32 * * * * * *   *  
AsShort 0 * * * * * *   *  
AsInt 0 * * * * * *   *  
AsLong 0 * * * * * *   *  
AsFloat 0.0 * * * * * *   *  
AsDouble 0.0 * * * * * *   *  
AsDate null invalid invalid * * * * * *  
AsString empty * * * * * * * * *
AsLongBinary NULL                 *
Empty cells indicate the conversion is not available, thus code asserts.
Cells marked with * indicate conversion is available (See the Conversion algorithms table for data conversion rules).

Conversions made by assignment operators

Database Field Type

Argument of the assignment operator

  bool unsigned char short int long float double COleDateTime String
NULL                  
BOOL * * * * * * *   *
UCHAR * * * * * * *   *
SHORT * * * * * * *   *
LONG * * * * * * *   *
SINGLE * * * * * * *   *
DOUBLE * * * * * * *   *
DATE               * *
STRING * * * * * * * * *
BINARY                  
Empty cells indicate the conversion is not available, thus code asserts.
Cells marked with * indicate conversion is available (See the Conversion algorithms table for data conversion rules).
Conversion algorithms
String to Bool comparing the first character of the string with 'T'
Char to Bool comparing the character with 'T'
Bool to String String = (bVal) ? 'T' : 'F'
Bool to Char Char = (bVal) ? 'T' : 'F'
String to Number appropriate atoX() function
Number to String CString::Format() method using the appropriate format specifier string
String to Date COleDateTime::ParseDateTime() method
Date to String COleDateTime::Format() method

Examples of How to Use CODBCRecordset

You should include the files ODBCRecordset.h and ODBCRecordset.cpp in your project.

I usually include this line in my StdAfx.h file.

SQL
#include "ODBCRecordset.h"

Here is a simple code showing how CODBCRecordset can be used.

SQL
/////////////////////////////////////////////////////////////////////////////
CDatabase    db;
//    This connect string will pop up the ODBC connect dialog
CString        cConnect = "ODBC;";
db.Open( NULL,                //    DSN
     FALSE,                //    Exclusive
     FALSE,                //    ReadOnly
     cConnect,            //    ODBC Connect string
     TRUE                //    Use cursor lib
   );

COleDateTime    dOrderDate;

CODBCRecordset    rs( &db );
rs.Open( "SELECT * FROM Orders \
    WHERE ORDER_DATE > 'jan 1 2000' \
    ORDER BY ORDER_DATE" );
for( ; ! rs.IsEOF(); rs.MoveNext() )
{
//    The choice is yours. You may choose whatever way
//    you want to get the values
//
    //    These return COleDateTime value
    dOrderDate = rs.GetDate( "ORDER_DATE" );
    dOrderDate = rs.Field("ORDER_DATE").AsDate();

    //    These make implicit call to AsDate()
    dOrderDate = rs("ORDER_DATE");
    dOrderDate = rs.Field("ORDER_DATE");

    //    Now edit the fields in the recordset
    rs.Edit();
    rs("ORDER_DATE") = "jan 1 1999";    // Implicit conversion
    rs.Field("ORDER_DATE") = "jan 1 1999";  // Implicit conversion
    rs.Update();
}    //    for(....
//////////////////////////////////////////////////////////////////////////////

If ORDER_DATE is stored in the database as datetime or compatible data type, the value will be got directly.
If ORDER_DATE is stored in the database as string or compatible data type (char, varchar) the value will be converted via COleDateTime::ParseDateTime() method. If conversion fails, dOrderDate will be set to COleDatetime::invalid.

When opening a resultset generated by join statements, it is possible to get 2 or more columns that have the same name. CODBCRecordset leaves the name of the first column intact but other repeated columns are renamed with adding the number this columns repeats the name. Not repeated column names are left intact. E.g.

SQL
SELECT * FROM Orders, Customers WHERE Orders.CUST_ID = Customers.ID

If the table Orders have a column with name ID and Customers have a column with name ID, CODBCRecordset will rename ID from Customers to ID2 and all other not repeating column names will be intact.

Well, here is a tip: Rename columns manually to be sure what name they have, e.g.:

SQL
SELECT Orders.*, Customers.ID as CUSTOMERS_ID
    FROM Orders, Customers 
    WHERE Orders.CUST_ID = Customers.ID

How Does CODBCRecordset Work?

CODBCRecordset allocates storage for all fields in the resultset and uses MFC Record Field eXchange mechanism like it has been inherited from CRecordset using ClassWizard.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Brosix
Bulgaria Bulgaria
Stefan does programming since his early ages more than 30 years ago. Later he began programming at his work and now he is very happy to work his hobby. He owns a Master's degree in Computer Science.

During his professional career, Stefan has worked with many technologies. His programming experience includes C/C++, Java, C#, PHP, JavaScript, MFC, ATL, ASP, ASP.NET, TCP/IP, SQL. He has worked with different operating systems: Windows, Linux, Mac OS X, iOS, Android, Solaris, FreeBSD, NetBSD and QNX.

Currently his professional interests are in building large scale distributed systems that operate over the Internet. This involves building server components as well as developing system level software.

More information about his current work can be found here: brosix.com - Enterprise Instant Messaging


Stefan is based in Plovdiv, Bulgaria. It is a very nice and peaceful place combined with an enjoyable weather.

Stefan has a wife and 2 children. He likes in his spear time to travel with his family to see new and interesting places.

Comments and Discussions

 
GeneralRe: Connection Dialog Pin
McNito24-Aug-04 14:00
McNito24-Aug-04 14:00 
GeneralRe: Connection Dialog Pin
Anonymous18-May-05 6:38
Anonymous18-May-05 6:38 
GeneralParameters don't work Pin
Srikanth Garlapati18-Jan-04 22:49
Srikanth Garlapati18-Jan-04 22:49 
GeneralMessage in output window Pin
FASTian12-Jan-04 22:17
FASTian12-Jan-04 22:17 
GeneralUNICODE Pin
fill14-Dec-03 22:27
fill14-Dec-03 22:27 
GeneralRe: UNICODE Pin
Anonymous7-Feb-05 23:47
Anonymous7-Feb-05 23:47 
GeneralRe: UNICODE Pin
El.Protestanto10-Apr-06 3:59
El.Protestanto10-Apr-06 3:59 
QuestionHow to realize the parameter query? Pin
Scott An20-Jul-03 16:46
Scott An20-Jul-03 16:46 
Thank you in advance.

Best Regards.
Questionhow to insert a new record into an empty table? Pin
redark11-Jul-03 16:30
redark11-Jul-03 16:30 
AnswerRe: how to insert a new record into an empty table? Pin
John M. Drescher11-Jul-03 18:51
John M. Drescher11-Jul-03 18:51 
GeneralRe: how to insert a new record into an empty table? Pin
redark11-Jul-03 22:02
redark11-Jul-03 22:02 
Generalurgent help Pin
michelle54228-Jul-03 6:07
michelle54228-Jul-03 6:07 
GeneralRe: urgent help Pin
cr971-Aug-03 0:45
cr971-Aug-03 0:45 
Generali got problems Pin
alicedemon17-Jun-03 7:19
alicedemon17-Jun-03 7:19 
GeneralRe: i got problems Pin
cr971-Aug-03 0:49
cr971-Aug-03 0:49 
GeneralStrange bug Pin
hkulten11-Jun-03 2:22
hkulten11-Jun-03 2:22 
GeneralHandling of RGB type Pin
Mate3-Jun-03 13:46
Mate3-Jun-03 13:46 
GeneralRe: Handling of RGB type Pin
John M. Drescher3-Jun-03 14:50
John M. Drescher3-Jun-03 14:50 
GeneralProblem when deleting a record Pin
ttohme15-May-03 6:30
ttohme15-May-03 6:30 
GeneralRe: Problem when deleting a record Pin
trelliot12-Jun-03 16:28
trelliot12-Jun-03 16:28 
Questionis it OK to open a excel file and read it ? Pin
etrans10-May-03 14:04
etrans10-May-03 14:04 
GeneralProblem with '*' in query Pin
wacek18-Apr-03 2:52
wacek18-Apr-03 2:52 
GeneralUsing Binary Pin
naveed17-Apr-03 5:42
naveed17-Apr-03 5:42 
GeneralRe: Using Binary Pin
Trapper14-Nov-03 6:18
Trapper14-Nov-03 6:18 
GeneralGreat Class Pin
josus115-Apr-03 3:37
josus115-Apr-03 3:37 

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.