A set of ODBC Classes






4.90/5 (22 votes)
Aug 15, 2001
5 min read

501725

3234
Two classes that make easy to work with ODBC
- Overview
- The CODBCDatabase Class
- CODBCDatabase
- Open
- DriverConnect
- SetReadOnly
- SetLoginTimeout
- SetConnectionTimeout
- GetConnectionTimeout
- Execute
- GetRowsAffected
- IsConnected
- Close
- The CODBCRecordset Class
- CODBCRecordset
- Open
- GetFieldLength
- GetFieldIndex
- GetFieldName
- GetFieldAttributes
- GetFieldCount
- GetFieldValue
- MoveFirst
- MoveNext
- MovePrevious
- MoveLast
- IsEof
- IsBof
- Close
Overview
I created this classes to make it easy to work
with ODBC. For this I created the CODBCDatabase
and CODBCRecordset
classes
Constructs a CODBCDatabase object.
CODBCDatabase();
The Open establishes connections to a driver and a data source.
BOOL Open(CHAR* lpstrDSN, CHAR* lpstrUser = NULL, CHAR* lpstrPass = NULL);
Parameters
CHAR* lpstrDSN
Pointer to a null-terminated string containing the data source name
CHAR* lpstrUser = NULL
Pointer to a null-terminated string containing the user identifier
CHAR* lpstrPass = NULL
Pointer to a null-terminated string containing the authentication string (typically the password).
If the function succeeds, this returns TRUE.
DriverConnect supports data sources that require more connection information than the three arguments in Open function, dialog boxes to prompt the user for all connection information, and data sources that are not defined in the system information.
BOOL DriverConnect(CHAR* szConnStr, CHAR* szConnStrOut = NULL, HWND hWnd = NULL, enum drvCompletion drvConn = sqlNoPrompt);
Parameters
CHAR* szConnStr
Pointer to a null-terminated string containing a full connection string (see the syntax in "Comments"), a partial connection string, or an empty string.
CHAR* szConnStrOut = NULL
Pointer to a buffer for the completed connection string.
HWND hWnd = NULL
Window handle. The application can pass the handle of the parent window, if applicable, or a null pointer
if either the window handle is not applicable or DriverConnect will not present any dialog boxes.
enum drvCompletion drvConn
Flag that indicates whether the Driver Manager or driver must prompt for more connection information:
CODBCDatabase::sqlNoPrompt
CODBCDatabase::sqlPrompt
If the function succeeds, this returns TRUE.
The SetReadOnly indicator that the connection is not required to support SQL statements that cause updates to occur.
void SetReadOnly(BOOL bReadOnly = TRUE);
Parameters
BOOL bReadOnly = TRUE
Set read only
If the function succeeds, this returns TRUE.
CODBCDatabase::SetLoginTimeout
The SetLoginTimeout set the number of seconds to wait for a login request to complete before returning to the application.
void SetLoginTimeout(LONG nSeconds);
Parameters
LONG nSeconds
The time in seconds. If it is 0, the timeout is disabled and a connection attempt will wait indefinitely.
CODBCDatabase::SetConnectionTimeout
The SetConnectionTimeout set the number of seconds to wait for any request on the connection to complete before returning to the application.
void SetConnectionTimeout(LONG nSeconds);
Parameters
LONG nSeconds
The time in seconds
CODBCDatabase::GetConnectionTimeout
The GetConnectionTimeout returns the time n seconds of connection timeout.
LONG GetConnectionTimeout();
The Execute function executes a SQL preparable statement.
BOOL Execute(CHAR* szSqlStr);
Parameters
CHAR* szSqlStr
If the function succeeds, this returns TRUE.
CODBCDatabase pdb; if(!pdb.DriverConnect("DSN=Test;SERVER=ServerSQL;UID=;PWD=;DATABASE=TestDatabase;")) return FALSE; pdb.Execute("Delete Test Where Field1 >= 17"); n = pdb.GetRowsAffected();
CODBCDatabase::GetRowsAffected
The GetRowsAffected returns the number of rows affected for the last Execute.
int GetRowsAffected();
The IsConnected returns true
if the connection to database is open.
BOOL IsConnected();
The Close function closes the connection.
void Close();
CODBCRecordset::CODBCRecordset
Constructs a CODBCRecordset object.
CODBCRecordset(CODBCDatabase* pDb);
Parameters
CODBCDatabase* pDb
Pointer to the CODBCDatabase Object.
The Open executes a preparable SQL statement.
BOOL Open(CHAR* szSqlStr);
Parameters
CHAR* szSqlStr
Pointer to a null-terminated string containing a SQL statement.
CODBCRecordset::GetFieldLength
The GetFieldLength returns the size of the Field on the data source.
LONG GetFieldLength(int nField);
Parameters
int nField
Field Index of result data, ordered sequentially in increasing Field order, starting at 0
The GetFieldIndex returns the index of a specific field name. If the field name don't exist GetFieldIndex
returns -1.
int GetFieldIndex(CHAR* szFieldName);
Parameters
CHAR* szFieldName
Pointer to a null-terminated string containing the Field Name.
The GetFieldName returns the field name in the result set.
BOOL GetFieldName(int nField, CHAR* szFieldName);
Parameters
int nField
CHAR* szFieldName
Pointer to a buffer in which to return the Field Name.
CODBCRecordset::GetFieldAttributes
The GetFieldAttributes returns the result descriptor—field name, type, field size-for one column in the result set.
BOOL GetFieldAttributes(int nField, CHAR* szFieldName, int& nType, int& nLength);
Parameters
int nField
Field Index of result data, ordered sequentially in increasing Field order, starting at 0
CHAR* szFieldName
Pointer to a buffer in which to return the Field Name.
int& nType
Pointer to a buffer in which to return the SQL Data Type of the Field.
int& nLength
Pointer to a buffer in which to return the size of the Field on the data source.
CODBCRecordset::typeChar
CODBCRecordset::typeVarChar
CODBCRecordset::typeLongVarChar
CODBCRecordset::typeWChar
CODBCRecordset::typeWVarChar
CODBCRecordset::typeWLongVarChar
CODBCRecordset::typeDecimal
CODBCRecordset::typeNumeric
CODBCRecordset::typeSmallint
CODBCRecordset::typeInteger
CODBCRecordset::typeReal
CODBCRecordset::typeFloat
CODBCRecordset::typeDouble
CODBCRecordset::typeBit
CODBCRecordset::typeTinyint
CODBCRecordset::typeBigInt
CODBCRecordset::typeBinary
CODBCRecordset::typeVarBinary
CODBCRecordset::typeLongVarBinary
CODBCRecordset::typeDate
CODBCRecordset::typeTime
CODBCRecordset::typeTimeStamp
CODBCRecordset::typeIntervalMonth
CODBCRecordset::typeIntervalYear
CODBCRecordset::typeIntervalYearToMonth
CODBCRecordset::typeIntervalDay
CODBCRecordset::typeIntervalHour
CODBCRecordset::typeIntervalMinute
CODBCRecordset::typeIntervalSecond
CODBCRecordset::typeIntervalDayToHour
CODBCRecordset::typeIntervalDayToMinute
CODBCRecordset::typeIntervalDayToSecond
CODBCRecordset::typeIntervalHourToMinute
CODBCRecordset::typeIntervalHourToSecond
CODBCRecordset::typeIntervalMinuteToSecond
CODBCRecordset::typeGUID
The GetFieldCount returns the number of fields in the result set.
int GetFieldCount();
The GetFieldValue.
BOOL GetFieldValue(int nField, CHAR* szData); BOOL GetFieldValue(CHAR* szFieldName, CHAR *szData); BOOL GetFieldValue(int nField, LONG *lData); BOOL GetFieldValue(CHAR* szFieldName, LONG *lData); BOOL GetFieldValue(int nField, DOUBLE *dblData); BOOL GetFieldValue(CHAR* szFieldName, DOUBLE *dblData); BOOL GetFieldValue(int nField, struct tm* time); BOOL GetFieldValue(CHAR* szFieldName, struct tm* time);
Parameters
int nField
Field Index of result data, ordered sequentially in increasing Field order,
starting at 0
CHAR* szFieldName
Pointer to a null-terminated string containing the Field Name
CHAR* szData
Pointer to the buffer in which to return the data.
LONG *lData
Pointer to a null-terminated string buffer in which to return the data.
DOUBLE *dblData
Pointer to a double buffer in which to return the data.
struct tm* time
Pointer to a time struct buffer in which to return the data
If the function succeeds, this returns TRUE.
The MoveFirst makes the First record of the recordset the current record.
BOOL MoveFirst();
If the function succeeds, this returns TRUE.
The MoveNext makes the Next record of the recordset the current record.
BOOL MoveNext();
If the function succeeds, this returns TRUE.
The MovePrevious makes the Previous record of the recordset the current record.
BOOL MovePrevious();
If the function succeeds, this returns TRUE.
The MoveLast makes the Last record of the recordset the current record.
BOOL MoveLast();
If the function succeeds, this returns TRUE.
The IsEof returns true
if the current position contains no records.
BOOL IsEof();
If the function succeeds, this returns TRUE.
The IsBof returns true
if the current position is the bottom of the recordset.
BOOL IsBof();
If the function succeeds, this returns TRUE.
The Close.
void Close();
Sample:
CODBCDatabase pdb; pdb.SetConnectionTimeout(15); if(!pdb.DriverConnect("DSN=Test;SERVER=ServerSQL;UID=;PWD=;DATABASE=TestDatabase;")) return FALSE; CODBCRecordset prs = CODBCRecordset(&pdb); prs.Open("Select * From test"); char szData[256]; while(!prs.IsEof()) { int nType; long lData; double dblData; int nLen; struct tm time; prs.GetFieldAttributes(0, NULL, nType, nLen); switch(nType) { case CODBCRecordset::typeChar: case CODBCRecordset::typeVarChar: memset(szData, 0, sizeof(szData)); prs.GetFieldValue(0, szData); break; } prs.GetFieldValue(1, &lData); memset(szData, 0, sizeof(szData)); prs.GetFieldValue(2, szData); prs.GetFieldValue(3, &time); memset(szData, 0, sizeof(szData)); prs.GetFieldValue(4, szData); dblData = 0; prs.GetFieldValue(5, &dblData); dblData = 0; prs.GetFieldValue(6, &dblData); prs.MoveNext(); } prs.Close(); pdb.Close();
Carlos A. Antollini.
Updates
14 August 2001 Version 1.0 Released.
29 August 2001 Updated source files.
Special thanks
The CODBC* class have received many suggestions from the readers. Thank to All for your colaboration.