Click here to Skip to main content
15,886,788 members
Articles / Database Development / SQL Server
Article

Building a Database Application using OLE DB

Rate me:
Please Sign up or sign in to vote.
1.67/5 (15 votes)
11 May 20041 min read 52.1K   2K   28   6
An article on building a Database Application with OLE DB

Introduction

This article, hopefully will show you how to build a database application with OLE DB. I have tried to make database applications using different ways, like ODBC, ADO, and now OLE DB. Let's get started!

Using the code

We will be needing a database for this tutorial. That's why, I have included a very simple sample of it (data.mdb). You will need to set up a ODBC data source for this. In this article, we will be using an ATL object, you can select it from Insert > New ATL Object > Data Access > Consumer. It will ask you for a data source, select the data source which you have made with ODBC. Because we will be making some changes on the database, check the Change, Insert, Delete support. Click OK and that choose the employee table.

Until now, you haven't write any single code, but actually you have done quite a lot. The wizard will create two class:

  • CEmployee
  • CEmployeeAccessor

You will do a lot on the CEmployee class, like viewing record, adding, and delete.

To access a record, you can write something like this:

CEmployee data;
data.m_id; //to access the id field in employee

To move a record:

CEmployee data;
data.MoveNext();
data.MovePrev();
but before that, you need to set the Database Property Set, so that the MovePrev() will work. Add the code as follow:
CDBPropSet propset(DBPROPSET_ROWSET);
propset.AddProperty(DBPROP_CANFETCHBACKWARDS, true); 
  //add this code to set DBPROP_CANFETCHBACKWARDS to true
propset.AddProperty(DBPROP_IRowsetChange, true);
propset.AddProperty(DBPROP_UPDATABILITY, DBPROPVAL_UP_CHANGE 
  | DBPROPVAL_UP_INSERT | DBPROPVAL_UP_DELETE);
From the parameter DBPROP_CANFETCHBACKWARDS, you can see that the Record Set can move backward, if it is set to true.

To add a record

CEmployee data;    
strcpy(data.m_id,"3"); //set the id field with "3"
strcpy(data.m_name,"Tony"); //set the name field with "Tony"
data.Insert(); //this will be added as the last record 

To update a record

CEmployee data;
strcpy(data.m_id,"3"); //set the id field with "3"
strcpy(data.m_name,"Tony"); //set the name field with "Tony"
data.SetData(); //Update the record

To delete a record

CEmployee data;
data.Delete();

That's it, you have just made yourself a simple database application using OLE DB. Pretty simple huh, but beyond that, it's not that simple.

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


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

Comments and Discussions

 
QuestionIt seems some wrong with it Pin
pigkiller129-May-07 17:54
pigkiller129-May-07 17:54 
GeneralInserting BLOB graphic files into a Word doc Pin
mckly30-Jun-04 4:27
mckly30-Jun-04 4:27 
GeneralInstructions on Using Code Pin
Yogesh Kshatriya18-Jun-04 0:51
Yogesh Kshatriya18-Jun-04 0:51 
GeneralYou should have labelled your article ... Pin
WREY13-May-04 1:30
WREY13-May-04 1:30 
GeneralRe: You should have labelled your article ... Pin
Yulianto.17-May-04 21:36
Yulianto.17-May-04 21:36 
GeneralComments in code Pin
dandy7212-May-04 9:48
dandy7212-May-04 9:48 

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.