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
You will do a lot on the
CEmployee class, like viewing record, adding, and
To access a record, you can write something like this:
To move a record:
but before that, you need to set the Database Property Set, so that the
will work. Add the code as follow:
| DBPROPVAL_UP_INSERT | DBPROPVAL_UP_DELETE);
From the parameter
, you can see that the Record Set can move backward,
if it is set to true.
To add a record
strcpy(data.m_id,"3"); strcpy(data.m_name,"Tony"); data.Insert();
To update a record
strcpy(data.m_id,"3"); strcpy(data.m_name,"Tony"); data.SetData();
To delete a record
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.