Click here to Skip to main content
11,493,154 members (58,071 online)
Click here to Skip to main content

EasyODBC

, 19 Aug 2003 BSD 103.3K 4.8K 61
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

No Biography provided

Comments and Discussions

 
Questionconnect to mdf file Pin
Mahdi Nejadsahebi18-Dec-12 4:55
memberMahdi Nejadsahebi18-Dec-12 4:55 
GeneralPossible memory leake Pin
Member 21093473-Sep-09 5:23
memberMember 21093473-Sep-09 5:23 
GeneralAccess Violation in release mode Pin
lorenzopapa27-Mar-09 1:28
memberlorenzopapa27-Mar-09 1:28 
GeneralRelease Mode Problem Pin
wsabol9-Feb-09 15:15
memberwsabol9-Feb-09 15:15 
GeneralRe: Release Mode Problem Pin
Vijay Mathew Pandyalakal9-Feb-09 19:25
memberVijay Mathew Pandyalakal9-Feb-09 19:25 
GeneralRe: Release Mode Problem Pin
appforce10-Feb-09 14:03
memberappforce10-Feb-09 14:03 
GeneralRe: Release Mode Problem Pin
Vijay Mathew Pandyalakal10-Feb-09 19:25
memberVijay Mathew Pandyalakal10-Feb-09 19:25 
GeneralRe: Release Mode Problem Pin
appforce11-Feb-09 11:11
memberappforce11-Feb-09 11:11 
GeneralDouble/Integer fields Pin
scsofts22-Dec-08 20:10
memberscsofts22-Dec-08 20:10 
GeneralRe: Double/Integer fields Pin
Vijay Mathew Pandyalakal23-Dec-08 2:12
memberVijay Mathew Pandyalakal23-Dec-08 2:12 
QuestionProblem in EasyODBCException class? Pin
Member 41644897-Sep-08 21:26
memberMember 41644897-Sep-08 21:26 
QuestionThe Program works almost perfect Pin
Member 456387811-Mar-08 9:31
memberMember 456387811-Mar-08 9:31 
Generalerror in sql Pin
rasha7615-Sep-04 22:48
memberrasha7615-Sep-04 22:48 
GeneralRe: error in sql Pin
Vijay Mathew Pandyalakal15-Sep-04 23:35
memberVijay Mathew Pandyalakal15-Sep-04 23:35 
Generalerror in retriving data Pin
rasha7610-Sep-04 0:21
memberrasha7610-Sep-04 0:21 
GeneralRe: error in retriving data Pin
Vijay Mathew Pandyalakal11-Sep-04 3:35
memberVijay Mathew Pandyalakal11-Sep-04 3:35 
GeneralDatabase::Open - BUG Pin
RSabet13-Aug-04 3:42
memberRSabet13-Aug-04 3:42 
GeneralRe: Database::Open - BUG Pin
RSabet13-Aug-04 3:58
memberRSabet13-Aug-04 3:58 
GeneralDemo crash Pin
Don Kim14-Apr-04 3:50
sussDon Kim14-Apr-04 3:50 
GeneralRe: Demo crash Pin
Don Kim14-Apr-04 4:33
memberDon Kim14-Apr-04 4:33 
GeneralCode crashes with faulty SQL instruction Pin
mace26-Jan-04 5:16
membermace26-Jan-04 5:16 
GeneralThanks a lot! Pin
xu_yangmu6-Jan-04 22:56
memberxu_yangmu6-Jan-04 22:56 
GeneralExcelllent code! Pin
vasacheh23-Dec-03 9:43
membervasacheh23-Dec-03 9:43 
QuestionHow Can We perform exclusive lock Pin
Benj104-Dec-03 13:16
memberBenj104-Dec-03 13:16 
QuestionClosing a Cursor ? Pin
Anonymous9-Sep-03 10:38
sussAnonymous9-Sep-03 10:38 
AnswerRe: Closing a Cursor ? Pin
Anonymous10-Sep-03 3:36
sussAnonymous10-Sep-03 3:36 
AnswerRe: Closing a Cursor ? Pin
abenlin9-Jan-04 21:24
memberabenlin9-Jan-04 21:24 
GeneralRe: Closing a Cursor ? Pin
Vijay Mathew Pandyalakal12-Jan-04 1:43
memberVijay Mathew Pandyalakal12-Jan-04 1:43 
GeneralRe: Closing a Cursor ? Pin
Kevin Tucker16-Jun-05 13:54
memberKevin Tucker16-Jun-05 13:54 
QuestionHow to get # of rows Pin
prcarp1-Sep-03 5:14
memberprcarp1-Sep-03 5:14 
GeneralNulls Pin
brianma27-Aug-03 3:02
memberbrianma27-Aug-03 3:02 
GeneralRe: Nulls Pin
kragg28-Dec-03 9:08
memberkragg28-Dec-03 9:08 

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
Web01 | 2.8.150520.1 | Last Updated 20 Aug 2003
Article Copyright 2003 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid