Click here to Skip to main content
Licence BSD
First Posted 19 Aug 2003
Views 85,757
Downloads 3,260
Bookmarked 55 times

EasyODBC

By AnOldGreenHorn | 19 Aug 2003
An easy to use C++ wrapper class for ODBC.
 
Part of The SQL Zone sponsored by
See Also
2 votes, 8.7%
1

2
3 votes, 13.0%
3
2 votes, 8.7%
4
16 votes, 69.6%
5
4.69/5 - 23 votes
2 removed
μ 4.19, σa 2.24 [?]

Introduction

MFC provides wrapper classes like CDatabase and CResultSet for accessing databases from your C++ applications. But if you want to avoid using MFC in your application, then you have only one option - call low level ODBC functions. EasyODBC is a C++ code library that provides simple classes that encapsulate Win32 ODBC functions.

Background

For using the classes in EasyODBC, you should have a basic knowledge on databases and SQL. It is also expected that you have working knowledge of C++ and the Visual C++ IDE. If you are familiar with JDBC (Java Database Connectivity), then the classes in EasyODBC will seem more easy to use.

Using the code

For using EasyODBC, first include easy_odbc.h using the #include preprocessor directive. All classes in EasyODBC are part of a namespace called easyodbc.

To open a connection to an ODBC data source, you have to create an object of class Database and call its Open() member function:

easyodbc::Database db;
db.Open("MY_ODBC","username","password");

For retrieving data from the database, you have to use the ResultSet class:

easyodbc::ResultSet rslt = db.ExecuteQuery("SELECT * FROM emp");

Information about the resultset, like the number of columns in it, can be obtained by calling the GetMetaData() function, which returns an object of ResultSetMetaData.

easyodbc::ResultSetMetaData mtdt = rslt.GetMetaData();
int column_count = mtdt.GetColumnCount();
printf("\n%d Columns returned\n",column_count);

For getting information about a particular column, call the GetColumn member function of ResultSetMetaData:

easyodbc::Column col;
mtdt.GetColumn(1,&col);

Data stored in a ResultSet has to be bound to memory buffers for retrieval. This is done using the Bind() function of ResultSet:

char strName[26];
rslt.Bind(1,strName,25); // arguments: column number, 
                         // buffer, maximum length of buffer

The above code binds the first column in the table with the buffer strName. Now, whenever data is returned by the ResultSet, the first column's data will be stored in the variable strName. Data can be pulled out of the ResultSet object by calling either the MoveFirst(), MoveNext(), MovePrevious() or MoveLast() member functions. For e.g., the following code prints out the value of the first column in the ResultSet:

while(rslt.MoveNext()) {
    printf("%s\n",strName);
}

For executing any other SQL statements, you have to call the Execute() member function of Database:

db.Execute("DELETE FROM emp");

The Exceute() function will return the number of rows affected by the statement. After database operations are over, you must release the resources occupied by ODBC by calling the Close() function of the class Database.

EasyODBC provides the EasyODBCException class to handle ODBC exceptions. All EasyODBC code should be enclosed in a try-catch block like this:

try {
// easy_odbc code
}catch(easyodbc::EasyODBCException *ex) {
 char buff[51];
 ex->GetMessage(buff);
 printf("Error: %s",buff);
}

In the demo project you will find a complete working program which demonstrates the use of various EasyODBC classes.

History

  • Created: May 27th, 2003
  • Last updated: August 12th, 2003

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

AnOldGreenHorn



India India

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPossible memory leake PinmemberMember 21093475:23 3 Sep '09  
GeneralAccess Violation in release mode Pinmemberlorenzopapa1:28 27 Mar '09  
GeneralRelease Mode Problem Pinmemberwsabol15:15 9 Feb '09  
GeneralRe: Release Mode Problem PinmemberVijay Mathew Pandyalakal19:25 9 Feb '09  
GeneralRe: Release Mode Problem Pinmemberappforce14:03 10 Feb '09  
GeneralRe: Release Mode Problem PinmemberVijay Mathew Pandyalakal19:25 10 Feb '09  
GeneralRe: Release Mode Problem Pinmemberappforce11:11 11 Feb '09  
GeneralDouble/Integer fields Pinmemberscsofts20:10 22 Dec '08  
GeneralRe: Double/Integer fields PinmemberVijay Mathew Pandyalakal2:12 23 Dec '08  
QuestionProblem in EasyODBCException class? PinmemberMember 416448921:26 7 Sep '08  
QuestionThe Program works almost perfect PinmemberMember 45638789:31 11 Mar '08  
Generalerror in sql Pinmemberrasha7622:48 15 Sep '04  
GeneralRe: error in sql PinmemberVijay Mathew Pandyalakal23:35 15 Sep '04  
Generalerror in retriving data Pinmemberrasha760:21 10 Sep '04  
GeneralRe: error in retriving data PinmemberVijay Mathew Pandyalakal3:35 11 Sep '04  
GeneralDatabase::Open - BUG PinmemberRSabet3:42 13 Aug '04  
GeneralRe: Database::Open - BUG PinmemberRSabet3:58 13 Aug '04  
GeneralDemo crash PinsussDon Kim3:50 14 Apr '04  
GeneralRe: Demo crash PinmemberDon Kim4:33 14 Apr '04  
GeneralCode crashes with faulty SQL instruction Pinmembermace5:16 26 Jan '04  
GeneralThanks a lot! Pinmemberxu_yangmu22:56 6 Jan '04  
GeneralExcelllent code! Pinmembervasacheh9:43 23 Dec '03  
QuestionHow Can We perform exclusive lock PinmemberBenj1013:16 4 Dec '03  
QuestionClosing a Cursor ? PinsussAnonymous10:38 9 Sep '03  
AnswerRe: Closing a Cursor ? PinsussAnonymous3:36 10 Sep '03  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120209.1 | Last Updated 20 Aug 2003
Article Copyright 2003 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid