Click here to Skip to main content
Click here to Skip to main content

A set of ODBC Classes

, 27 Aug 2001
Rate this:
Please Sign up or sign in to vote.
Two classes that make easy to work with ODBC
<!-- Download Links --> <!-- Add the rest of your HTML here -->

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

<A name=Sample0>
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();
</A>

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:

<A name=TheSample>
	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();
</A>

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

Share

About the Author

Carlos Antollini
Architect Citigroup
Argentina Argentina
Carlos Antollini is a software engineer working on Object Oriented, Visual C++, MFC, COM, ATL, ADO, Internet technologies and Business Intelligence.
Carlos is originally from Argentina, he was living for several years in Fort Lauderdale, Florida, working for Citibank. Then he started his own business.
Carlos is the creator of piFive[^], a family of BI Analytic Platform software, that it deals next to, latinsys[^], his partner in businesses...
Currently he is sharing his passion for project management and BI at Citigroup.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberwuditanke25-Nov-11 23:52 
GeneralRe: My vote of 5 PinmemberMember 108654125-Jun-14 0:33 
GeneralHelp use these classes Pinmemberadekastos22-Dec-09 5:17 
Generalunicode PinmemberLove In Snowing27-Jan-08 17:56 
QuestionVERY URGENT PinmemberProgramm3r21-Aug-07 3:25 
GeneralI Like it .... PinmemberProgramm3r27-May-07 22:55 
GeneralThank you PinmemberYescode5-Aug-06 0:26 
QuestionProblems with ODBC and ntext in Visual c++ Pinmembershsh2110-Jul-06 21:08 
Generalthank your source code! But... Pinmemberfivehun21-May-06 4:55 
GeneralRe: thank your source code! But... Pinmember7649733328-Jun-09 22:59 
GeneralToo inefficient! PinmemberTydia-kun30-Sep-05 2:11 
Generaltoo many bugs!!!!!!!!!!!!!!!!!! Pinmembermiaolin7-Dec-04 3:17 
GeneralProblem while compiling the files in the DLL PinmemberMeenakshi Chockalingam15-Nov-04 20:59 
GeneralExcel ODBC problem Pinmember@LX9-Nov-04 12:54 
GeneralConnect without using DSN PinmemberThiagarajan Rajendran24-Oct-04 23:34 
GeneralRe: Connect without using DSN PinmemberThiagarajan Rajendran2-Nov-04 20:39 
GeneralProblems with retriveing ntext field data Pinmemberplaythegame17-Sep-04 0:20 
GeneralMicrosoft C++ Exception? help Pinmember9999kkk12-Feb-04 5:46 
Questionhow to access DBF files via ODBC and your class ? Pinmemberkostax19-Jan-04 0:14 
Questionhow to insert long char data into database? Pinmemberjeam4-Jan-04 21:16 
AnswerRe: how to insert long char data into database? Pinmemberjeam5-Feb-04 15:50 
Answerhow to select lob field? Pinmemberjeam5-Feb-04 21:00 
QuestionMemoryleak or what? Pinmemberoneleg18-Dec-03 23:50 
AnswerRe: Memoryleak or what? Pinmemberoneleg19-Dec-03 4:39 
GeneralRe: Memoryleak or what? PinmemberCarlos Antollini19-Dec-03 6:45 
GeneralODBC Class Execute Command PinmemberRandyCP9-Dec-03 10:47 
GeneralWorks once, not again Pinmemberoneleg5-Dec-03 5:12 
GeneralRe: Works once, not again PinmemberRandyCP10-Dec-03 0:33 
GeneralRe: Works once, not again Pinmemberoneleg10-Dec-03 23:39 
GeneralRecordCount Pinmembertiger0071-Sep-03 2:51 
GeneralRe: RecordCount Pinmembertiger0072-Sep-03 22:54 
GeneralGetFieldIndex error PinmemberAndrey33310-Jul-03 1:29 
GeneralSample PinmemberABC_Software4-Jun-03 6:54 
GeneralCODBCDatabase::Open parameters are considered 4 bytes long. PinmemberLucia I. Luiz21-May-03 5:54 
GeneralInserting/updating fields to Nulls via ODBC Pinmemberpeternolan911-Jan-03 12:07 
Generalaccess database on the web server Pinmemberrobinhood13-Sep-02 2:47 
GeneralRe: access database on the web server PinmemberStefan Pedersen11-Jan-03 13:15 
GeneralODBC Problem PinmemberDasa9-Sep-02 3:03 
Generalclass does not set eof and bof if recordset is empty PinsussRene Neidt10-Jul-02 7:16 
GeneralDriverConnect() fails with Oracle 8; works with MFC's CDatabase PinmemberRFID Chris24-Jun-02 14:50 
GeneralRe: DriverConnect() fails with Oracle 8; works with MFC's CDatabase Pinmemberjeanballe22-Aug-02 6:06 
GeneralRe: DriverConnect() fails with Oracle 8; works with MFC's CDatabase PinmemberRFID Chris23-Aug-02 13:44 
GeneralRe: DriverConnect() fails with Oracle 8; works with MFC's CDatabase PinmemberCarlos Antollini23-Aug-02 13:46 
GeneralGetFieldValue(int nField, struct tm* time) Memory Leak PinmemberAdam Hunter7-Jun-02 12:11 
GeneralError compiling PinmemberWard Steensels24-May-02 6:40 
GeneralRe: Error compiling PinmemberRandyCP10-Dec-03 7:40 
GeneralProblem: ntext field PinmemberHenry Schwab3-May-02 22:59 
GeneralNo. of records selected by a SELECT stmt PinmemberSharat30-Apr-02 5:59 
GeneralRe: No. of records selected by a SELECT stmt PinmemberAnonymous7-Jun-02 12:15 
GeneralAnother Suggestion PinmemberMel21-Apr-02 11:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 28 Aug 2001
Article Copyright 2001 by Carlos Antollini
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid