5,442,164 members and growing! (18,599 online)
Email Password   helpLost your password?
Languages » C++ / CLI » General     Beginner

OLE DB - First steps

By Nishant Sivakumar

Basic introduction to using OLE DB to insert, update and read records from a database
C++/CLI, VC7, C++, Windows, .NET, .NET 1.0VS.NET2002, Visual Studio, Dev

Posted: 10 Oct 2001
Updated: 1 Dec 2001
Views: 126,883
Bookmarked: 7 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
46 votes for this Article.
Popularity: 6.05 Rating: 3.64 out of 5
1 vote, 25.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
3 votes, 75.0%
5

Introduction

This article intends to introduce you to using OLE DB for database access. It does this by showing you how to use OLE DB to insert records into a database and then read back those records. Before you start there are some things you need to do first, like creating an MDB file.

Things to do first...

  • First create a new MS Access database called test.mdb and create a single table and call it 'main'.
  • Now add two fields to 'main' called 'Name' of type 'Text' and 'Age' of type 'Number'.
  • Copy test.mdb to d:\

Some concepts

In .NET, connections to databases and queries are achieved through data providers. The OLE DB .NET data provider is implemented through various classes within the System::Data::OleDb namespace. In this article we only examine three of these classes - OleDbConnection, OleDbCommand and OleDbDataReader. The OleDbConnection object represents a database connection. The OleDbCommand object wraps an SQL command that is performed on a database connection.  When we are making an INSERT or an UPDATE query on a database table, those two are the only classes we'll need. But when we are retrieving data from a table, we'll also need to use the OleDbDataReader class. This class allows us to browse through a row of records in a forward-only direction.

Inserting records code snippet

//Create the OleDbConnection object 

//and associate it with our database

OleDbConnection* conn = new OleDbConnection(
    "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\test.mdb");

//Open the database connection

conn->Open();

//Create an OleDbCommand object and

//pass it the SQL command and the OleDbConnection

//object to use to connect to the database

OleDbCommand* cmd = new OleDbCommand(sqlstr,conn);

//Execute the SQL command

cmd->ExecuteNonQuery();

//Close the connection to the database

conn->Close();

Inserting records is the simpler of the two processes. We create a connection using the OleDbConnection object, create an OleDbCommand object and associate it with the OleDbConnection object. Now we call the ExecuteNonQuery method, which will execute the SQL command we had passed to the OleDbCommand  constructor. We then close the connection.

Reading records code snippet

//Create the OleDbConnection object 

//and associate it with our database

OleDbConnection* conn = new OleDbConnection(
    "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\test.mdb");

//Open the database connection

conn->Open();

//Create an OleDbCommand object and

//pass it the SQL read query and the connection to use

OleDbCommand* cmd = new OleDbCommand(sqlstr,conn);

//Procure the OleDbDataReader object to browse the recordset 

OleDbDataReader* rdr = cmd->ExecuteReader();

//Keep reading records in the forward direction

while (rdr->Read())
{
   //Use one of the various methods available to read the data

   //Eg:- GetValue, GetValues, Item etc.

    . . .
    . . .    
}

//Close the connection to the database

conn->Close();

This is basically the same as far as creating the OleDbConnection and OleDbCommand  objects are concerned. But instead of calling ExecuteNonQuery directly, we call ExecuteReader which will return a OleDbDataReader object. We can use this OleDbDataReader object to browse through the recordset. Keep calling Read which will return false when it has finished the whole recordset. There are several ways to read from a recordset but I prefer get_Item which allows you to specify a field name opposed to other functions like GetValue which require us to pass the index of the field in the table which is a bad method in my opinion.

Console::WriteLine(rdr->get_Item("FullName"));

There is a sequel to this article on the use of bound controls with OLE DB which you can find here.

Revision History

  • Jul 04 2002 - Did a full redo of the article, added a sample project and now uses MC++ instead of C#
  • Dec 02 2001 - Changed the program for .Net beta 2.0

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

About the Author

Nishant Sivakumar


Sitebuilder, Mvp
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.

Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.

Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.
Location: United States United States

Other popular C++ / CLI articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralUsing Windows mobile 5 application - connect MySQL dbmemberinfinit08:37 20 Apr '07  
GeneralUsing this with Windows Mobile 5?memberAlexEvans22:37 2 Apr '07  
GeneralRe: Using this with Windows Mobile 5?mvpNishant Sivakumar3:33 3 Apr '07  
GeneralRe: Using this with Windows Mobile 5?memberAlexEvans12:18 3 Apr '07  
GeneralGreat Examplememberkstrat200110:43 28 Aug '06  
GeneralRe: Great ExamplestaffNishant Sivakumar11:02 28 Aug '06  
Generalif the *.mdb file is on other machinememberRitesh Kumar Verma1:30 16 Jun '03  
GeneralRun time exceptionmemberAnonymous21:49 15 Jun '02  
GeneralRe: Run time exceptionsubeditorNishant S3:27 19 Jul '02  
Generalhow do i pass the arguments to the smemberAnonymous7:32 12 Jun '02  
GeneralRe: how do i pass the arguments to the smemberNish - Native CPian15:15 12 Jun '02  
GeneralWhat is the advantage of this code?membera reader11:34 2 Dec '01  
GeneralRe: What is the advantage of this code?memberNish [BusterBoy]20:11 2 Dec '01  
GeneralUpdated for beta 2.0memberNish [BusterBoy]23:37 1 Dec '01  
GeneralADO namespacememberAnonymous81819:42 19 Nov '01  
GeneralRe: ADO namespacememberNish [BusterBoy]23:38 1 Dec '01  
GeneralObsoletememberMN6:47 15 Oct '01  
GeneralRe: ObsoletememberNish [BusterBoy]23:39 1 Dec '01  
Generalgood jobmemberJingo2:24 11 Oct '01  
GeneralRe: good jobmemberNish [BusterBoy]3:57 11 Oct '01  
GeneralYaar, you are the bestmemberAmita Buch13:32 13 Oct '01  
GeneralException handlingmemberJason Douglas2:10 11 Oct '01  
GeneralRe: Exception handlingmemberNish [BusterBoy]4:00 11 Oct '01  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Dec 2001
Editor: Nishant Sivakumar
Copyright 2001 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project