Introduction
This is a small update I made to the excellent set of ADO classes by Carlos Antollini. Though this might be a little less common than .NET classes, MFC is still very much in use and I hope the changes would be useful to someone.
Background
Dated back to 2005, the original classes did not account for 64 bit data and compilers. I have taken the liberty to add support for 64 bit data such as MS-SQL BIGINT
. I also have support for 64 bit binary data to 64 int conversion useful in situations where we must read MS-SQL ROWVERSION and similar type fields.
I have also added a handful of utility functions to make common tasks part of the classes. I did however kept the original code in place for the most part and insured to keep backward compatibility.
Of course, all of this is meant to be compiled as a 32 bit application. I might add more for 64 bit applications later, if necessary.
You might also have to get a new TLB for some operating system. It is included in this article and from Microsoft. (msado60_Backcompat_i386.tlb)
Using the Code
In this section, I will not repeat what Carlos described in detail in his original post but concentrate on my changes.
Tweaking the Header for the Host Operating System
Depending on the bit size of the host operating system, a different version of ADO is used. Make sure to define this value before including the header if needed:
#define _USE_WINX86_FOLDER_ //Use this when compiling on 64 bit PC
#include <ado2.h> //ADO wrapper class
New Stuff
First, support 64 bit data:
The GetFieldValue
functions returns a value that contains the value of a field.
BOOL GetFieldValue(LPCTSTR lpFieldName, INT64& lValue);
BOOL GetFieldValue(int nIndex, INT64& lValue);
BOOL GetFieldValue(LPCTSTR lpFieldName, UINT64& ulValue);
BOOL GetFieldValue(int nIndex, UINT64& ulValue);
The SetFieldValue
functions sets the value of a field.
BOOL SetFieldValue(int nIndex, INT64 lValue);
BOOL SetFieldValue(LPCTSTR lpFieldName, INT64 lValue);
BOOL SetFieldValue(int nIndex, UINT64 lValue);
BOOL SetFieldValue(LPCTSTR lpFieldName, UINT64 lValue);
New Utility Functions
Here are my new utility functions that wrap commonly used tasks.
The CADODatabase
class has a set of functions that correspond to the _ConnectionPtr
. To this class, I have added these:
bool supIsValidDBPtr
- Returns true
if the object is initialized and is ready to be used
- On a new pointer
(CADODatabase* pDB)
, pDB->supIsValidDBPtr()
should return false
- On a new initialized pointer
(pDB = new CADODatabase)
, pDB->supIsValidDBPtr()
should return true
bool supReopenDB
- Tries to close and reopen a database, making a test to insure connectivity with a "SELECT 1
" recordset bool supIsDBAccessible
- Test to see if the current DB object can be accessed and optionally test with a "SELECT 1
" recordset or try reopen
The CADORecordset
class has a set of functions that corresponds to the _RecordsetPtr
. To this class, I have added these:
- 64 bit support
bool supHasRecord()
- Simple way to check if the last query returned any rows bool supOpenHasRecord()
- Combines the Open
method and the above in a single call __int64 supDbRowVersionTo64(LPCSTR pszField)
- Returns a 64 bit representation of an SQL Server ROWVERSION field or similar type data
The new CADOTool
class has a set of static utility functions that wraps common tasks:
CADODatabase* pDB = NULL; bool bRc = supInitDatabase(&pDB, szConnectString, 10);
Sample
This sample tries to demonstrate basic usage of the classes with a fictitious database.
CString csConnect;
CString csSQL;
CADODatabase* pDB = NULL; CADORecordset* pRS = NULL; int iUpdated;
__int64 iID;
CString csName;
COleDateTime cdtBirth;
BOOL bActive;
if(!CADOTool::adoBuildConnection(csConnect))
{
OutputDebugString("You cancelled the dialog!\n");
return;
}
if(!CADOTool::supInitDatabase(&pDB, csConnect))
{
OutputDebugString("Somehow, the database cannot be opened!\n");
return;
}
csSQL = "SELECT ID, fldName, fldBirth, fldIsActive FROM tblUserData ORDER BY fldMemberDate DESC";
try{
if(pRS->supOpenHasRecord(csSQL)) {
do
{
pRS->GetFieldValue("ID", iID);
pRS->GetFieldValue("fldName", csName);
pRS->GetFieldValue("fldBirth", cdtBirth);
pRS->GetFieldValue("fldIsActive", bActive);
pRS->Edit();
pRS->SetFieldValue("fldIsActive", true);
pRS->Update();
pRS->MoveNext(); }while(!pRS->IsEOF());
}
else
{
if(pRS->IsOpen())
{
pRS->AddNew();
pRS->SetFieldValue("fldName", CString("First, Last"));
pRS->SetFieldValue("fldBirth", COleDateTime(1980, 10, 2, 0, 0, 0));
pRS->SetFieldValue("fldIsActive", TRUE);
pRS->Update();
pRS->GetFieldValue("ID", iID); }
}
if(pDB->Execute("UPDATE tblManager SET fldLastChech = GETDATE() WHERE ID = 3"))
{
iUpdated = pDB->GetRecordsAffected(); }
}
catch(CADOException& cEx)
{
OutputDebugString("Oups, got a database error!\n");
OutputDebugString(cEx.GetErrorMessage());
OutputDebugString("\n");
}
CADOTool::supCleanRS(&pRS); CADOTool::supCloseDB(&pDB);
History
- 7th May, 2013 - First release of my contributed code to the original 2.20
- 15th May, 2013 - Added example and fixed a function error (
supOpenHasRecord
)