CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Data >> Database


Classes for direct SQL calls with ODBC
Rating: none

Dave Merner (view profile)
August 6, 1998

.

During my current project, I found that I needed more functionality with my sql calls than CRecordset or CDAORecordset had to offer. These classes wrap the low-level ODBC API calls and act simular to a CRecordset, but allow me to execute any SQL statement without having to bind variables,etc.
(continued)




The main class for doing this is CSQLDirect. CSQLDirect has the following attributes/functions:

  • CSQLDirect::Connect - Connects to a datasource.
  • CSQLDirect::ExecuteSQL - this is the main function that is used for handling the SQL statement.
  • CSQLDirect::GetCol - Will return a column from a table in the resulting cursor.
  • CSQLDirect::GetError - Provides detailed error messages in case something went wrong.
  • CSQLDirect::GetColumnType - Provides information about a cursor's column.
  • CSQLDirect::Fetch - Will properly execute a SQLFetch command with error handling.
  • CSQLDirect::Close - Closes the connection to the datasource.

The other class CSQLColumn is a support class for CSQLDirect. Since multiple queries to a cursor's column will result in a NULL value being returned, I found it necessary to keep track of the columns as they are used. This is stored in a CPtrArray within CSQLDirect and is cleaned up after each time the cursor is requeried/closed.

An example of using this class to make SQL Calls:

CSQLDirect SQLDirect( "TestData" );

if( SQLDirect.ExecuteSQL( "SELECT * FROM Employee" )==SQL_SUCCESS ) {
	while( SQLDirect.Fetch()==SQL_SUCCESS ) {
		.
		// Do some stuff here
		.
	}
}

That's it!

The great thing about this class is you no longer have need for a huge assortment of CRecordset classes for every table/query.

Anyways I hope this can be of help to anyone that uses the site. Don't hesitate to give me a shout if anyone has any questions/comments.

Thanks for all the help that CodeGuru has given my over the last few months!

Download Source 3K

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
GetCol bug - Legacy CodeGuru (02/09/2004)
to execute stored procedures(oracle) in vc++ - Legacy CodeGuru (01/27/2004)
How to use the "SQLColumns()" in Visual Basic - Legacy CodeGuru (10/28/2003)
How to read and write binary data by ODBC API - Legacy CodeGuru (07/14/2003)
Anyone knows how to connect the mysql to my programme in Linux? - Legacy CodeGuru (07/10/2003)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)