Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / MFC
Article

EasyODBC

Rate me:
Please Sign up or sign in to vote.
4.72/5 (27 votes)
19 Aug 2003BSD2 min read 128.3K   5.5K   62   36
An easy to use C++ wrapper class for ODBC.

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Bugerror in the username and password Pin
omegavikhy15-May-21 19:24
omegavikhy15-May-21 19:24 
PraiseThanks for your great work ! Jean Pin
LittleDad3-Jan-19 21:08
LittleDad3-Jan-19 21:08 
GeneralMy vote of 5 Pin
Kranti Madineni9-Oct-17 0:30
Kranti Madineni9-Oct-17 0:30 
Questionconnect to mdf file Pin
Mahdi Nejadsahebi18-Dec-12 3:55
Mahdi Nejadsahebi18-Dec-12 3:55 
GeneralPossible memory leake Pin
Ruscoff3-Sep-09 4:23
Ruscoff3-Sep-09 4:23 
GeneralAccess Violation in release mode Pin
lorenzopapa27-Mar-09 0:28
lorenzopapa27-Mar-09 0:28 
GeneralRelease Mode Problem Pin
wsabol9-Feb-09 14:15
wsabol9-Feb-09 14:15 
GeneralRe: Release Mode Problem Pin
AnOldGreenHorn9-Feb-09 18:25
AnOldGreenHorn9-Feb-09 18:25 
GeneralRe: Release Mode Problem Pin
bigbby10-Feb-09 13:03
bigbby10-Feb-09 13:03 
GeneralRe: Release Mode Problem Pin
AnOldGreenHorn10-Feb-09 18:25
AnOldGreenHorn10-Feb-09 18:25 
GeneralRe: Release Mode Problem Pin
bigbby11-Feb-09 10:11
bigbby11-Feb-09 10:11 
GeneralDouble/Integer fields Pin
scsofts22-Dec-08 19:10
scsofts22-Dec-08 19:10 
GeneralRe: Double/Integer fields Pin
AnOldGreenHorn23-Dec-08 1:12
AnOldGreenHorn23-Dec-08 1:12 
QuestionProblem in EasyODBCException class? Pin
Member 41644897-Sep-08 20:26
Member 41644897-Sep-08 20:26 
QuestionThe Program works almost perfect Pin
Member 456387811-Mar-08 8:31
Member 456387811-Mar-08 8:31 
Generalerror in sql Pin
rasha7615-Sep-04 21:48
rasha7615-Sep-04 21:48 
GeneralRe: error in sql Pin
AnOldGreenHorn15-Sep-04 22:35
AnOldGreenHorn15-Sep-04 22:35 
Generalerror in retriving data Pin
rasha769-Sep-04 23:21
rasha769-Sep-04 23:21 
GeneralRe: error in retriving data Pin
AnOldGreenHorn11-Sep-04 2:35
AnOldGreenHorn11-Sep-04 2:35 
GeneralDatabase::Open - BUG Pin
RSabet13-Aug-04 2:42
RSabet13-Aug-04 2:42 
easy_odbc.cpp
(Line 22)

if(user == NULL) strcpy(user,"");
if(pw == NULL) strcpy(pw,"");

I don't think this code will work, so do not pass NULL as user or pw ...
GeneralRe: Database::Open - BUG Pin
RSabet13-Aug-04 2:58
RSabet13-Aug-04 2:58 
GeneralDemo crash Pin
Don Kim14-Apr-04 2:50
Don Kim14-Apr-04 2:50 
GeneralRe: Demo crash Pin
Don Kim14-Apr-04 3:33
Don Kim14-Apr-04 3:33 
GeneralCode crashes with faulty SQL instruction Pin
mace26-Jan-04 4:16
mace26-Jan-04 4:16 
GeneralRe: Code crashes with faulty SQL instruction Pin
Kranti Madineni10-Oct-17 20:59
Kranti Madineni10-Oct-17 20:59 

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

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