Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A set of ODBC Classes

0.00/5 (No votes)
27 Aug 2001 6  
Two classes that make easy to work with ODBC

Overview

I created this classes�to make it easy to work with ODBC. For this I created the CODBCDatabase and CODBCRecordset classes

The CODBCDatabase Class

CODBCDatabase::CODBCDatabase

Constructs a CODBCDatabase object.

CODBCDatabase();

See sample

CODBCDatabase::Open

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.

See sample

CODBCDatabase::DriverConnect

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* szConnStrPointer 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.

See sample

CODBCDatabase::SetReadOnly

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

See sample

CODBCDatabase::GetConnectionTimeout

The GetConnectionTimeout returns the time n seconds of connection timeout.

LONG GetConnectionTimeout();

CODBCDatabase::Execute

The Execute function executes a SQL preparable statement.

BOOL Execute(CHAR* szSqlStr);

Parameters

CHAR* szSqlStr

Pointer to a null-terminated string containing the SQL statement to be executed

If the function succeeds, this returns TRUE.

See sample


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();

See sample

CODBCDatabase::IsConnected

The IsConnected returns true if the connection to database is open.

BOOL IsConnected();

CODBCDatabase::Close

The Close function closes the connection.

void Close();

See sample

The CODBCRecordset Class

CODBCRecordset::CODBCRecordset

Constructs a CODBCRecordset object.

CODBCRecordset(CODBCDatabase* pDb);

Parameters

CODBCDatabase* pDb Pointer to the CODBCDatabase Object.

See sample

CODBCRecordset::Open

The Open executes a preparable SQL statement.

BOOL Open(CHAR* szSqlStr);

Parameters

CHAR* szSqlStr Pointer to a null-terminated string containing a SQL statement.

See sample

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

CODBCRecordset::GetFieldIndex

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.

CODBCRecordset::GetFieldName

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.

See sample

Data Types

����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

CODBCRecordset::GetFieldCount

The GetFieldCount returns the number of fields in the result set.

int GetFieldCount();

CODBCRecordset::GetFieldValue

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.

See sample

CODBCRecordset::MoveFirst

The MoveFirst makes the First record of the recordset the current record.

BOOL MoveFirst();

If the function succeeds, this returns TRUE.

CODBCRecordset::MoveNext

The MoveNext makes the Next record of the recordset the current record.

BOOL MoveNext();

If the function succeeds, this returns TRUE.

See sample

CODBCRecordset::MovePrevious

The MovePrevious makes the Previous record of the recordset the current record.

BOOL MovePrevious();

If the function succeeds, this returns TRUE.

CODBCRecordset::MoveLast

The MoveLast makes the Last record of the recordset the current record.

BOOL MoveLast();

If the function succeeds, this returns TRUE.

CODBCRecordset::IsEof

The IsEof returns true if the current position contains no records.

BOOL IsEof();

If the function succeeds, this returns TRUE.

See sample

CODBCRecordset::IsBof

The IsBof returns true if the current position is the bottom of the recordset.

BOOL IsBof();

If the function succeeds, this returns TRUE.

CODBCRecordset::Close

The Close.

void Close();

See sample

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.


License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here