5,445,109 members and growing! (14,828 online)
Email Password   helpLost your password?
Languages » C++ / CLI » General     Beginner

OLE DB - Bound controls

By Nishant Sivakumar

Shows how you can use data bound controls with OLE DB
C++/CLI, VC7, C++, Windows, .NET, .NET 1.0VS.NET2002, Visual Studio, Dev

Posted: 10 Oct 2001
Updated: 12 Nov 2001
Views: 158,903
Bookmarked: 17 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
125 votes for this Article.
Popularity: 7.91 Rating: 3.77 out of 5
1 vote, 12.5%
1
0 votes, 0.0%
2
1 vote, 12.5%
3
4 votes, 50.0%
4
2 votes, 25.0%
5

Introduction

This article is intended as a sequel to my article OLE DB - First steps. We take a look at using the data grid as a bound control. We'll see how to use the OleDbDataAdapter and the DataSet classes to populate a data grid from an MS Access database. We'll also see how we can update records.

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'.
  • Populate the table with some values
  • Copy test.mdb to d:\

Populating the grid from the DB

...

DataGrid* dg;
OleDbDataAdapter* da;
OleDbConnection* odc;
DataSet* ds;

...

//Create an OleDbConnection object and point 

//it to our MS Access database

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

//Create an OleDbDataAdapter object passing our

//SQL command and the OleDbConnection object

da = new OleDbDataAdapter("select * from main",odc); 

//Create a new DataSet object

ds = new DataSet();

//Fill up the DataSet object using the Fill method

//of the OleDbDataAdapter class

da->Fill(ds,"main");

//Set the DataSource property of our DataGrid

//to the corresponding table in our DataSet

dg->DataSource = ds->Tables->get_Item("main");

...

The OleDbDataAdapter class is a channel through which a DataSet object reads and writes data from the actual database. The DataSet object is an in-memory database cache. When we call the Fill method on our OleDbDataAdapter object, passing it the DataSet object, the DataSet object gets filled up with the data from the database. Populating the data grid is now quite easy. It has a DataSource property which we point to the table we want from our DataSet object.

Updating records through the datagrid

DataSet* dschanged = ds->GetChanges(DataRowState::Modified);
if(dschanged)
    da->Update(dschanged,"main"); 
else
    MessageBox::Show("Nothing to update");

We call GetChanges on our DataSet object and this returns  a copy of the DataSet containing all changes made to it since it was last loaded. We use an overload of the method that allows us to specify a filter on the DataSet returned. I have used the DataRowState::Modified filter which will return a DataSet object with all the rows in the data grid that were modified. Among other options, commonly used one include DataRowState::Added and DataRowState::Deleted. Now we call Update on the OleDbDataAdapter object which will call the required INSERT, UPDATE or DELETE queries to update the database.

For the Update method to work correctly we need to set the UpdateCommand property of the OleDbDataAdapter object. We create a new OleDbCommand object which represents the SQL command to execute on our data source. As you can see we can use pseudo variables like @Age and @Name. Of course, we need to add these variables to the Parameters property which is a collection of OleDbParameterCollection objects.

da->UpdateCommand = new OleDbCommand(
    "update main set age = @Age where name = @Name",odc); 

OleDbParameter* ageparam = da->UpdateCommand->Parameters->Add(
    "@Age",OleDbType::Integer);
    ageparam->SourceColumn = "Age";

da->UpdateCommand->Parameters->Add(
    "@Name",OleDbType::VarChar,50,"Name");

Similarly you'll need to setup the DeleteCommand and the InsertCommand properties before you can call Update for queries that require DELETE and INSERT SQL queries. Be careful when you convert your database field types to the corresponding OleDbType enumeration. I had some trouble with the Number-Long Integer field type used in MS Access. As you can see I've finally split up the call into two, first calling another overload of the Add method and then manually setting the SourceColumn property.

Revision History

  • Jul 08 2002 - Did a full redo of the article, added a sample project and now uses MC++ instead of C#

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 
QuestionRuntime error on VC 7.0memberMike Pliam6:51 25 Apr '07  
Generalhelpmemberzerocool3:13 6 Jan '03  
General33?adminChris Maunder1:14 8 Jul '02  
GeneralRe: 33?subeditorNishant S1:26 8 Jul '02  
GeneralArticlememberbjwoodburn1:31 2 Apr '02  
GeneralRe: ArticlememberNish [BusterBoy]2:47 6 May '02  
GeneralObsolete!memberMN7:12 15 Oct '01  
GeneralRe: Obsolete!memberNish [BusterBoy]20:23 12 Nov '01  
GeneralRe: Obsolete!memberFranz R.0:47 13 Nov '01  
GeneralRe: Obsolete!memberDamon Chitsaz6:17 13 Nov '01  
Generalover 100 people rated this article !!memberJohn Paul Goatier13:38 13 Oct '01  
GeneralRe: over 100 people rated this article !!memberNorm Almond5:43 13 Nov '01  
GeneralRe: over 100 people rated this article !!memberBill Steven14:34 13 Nov '01  
Generalfrom india?memberSandheepG10:46 12 Oct '01  
GeneralRe: from india?memberNikhil Dabas12:50 12 Oct '01  
GeneralRe: from india?memberMichael Dunn18:54 12 Oct '01  
GeneralRe: from india?memberChristian Graus22:55 12 Nov '01  
GeneralRe: from india?memberNish [BusterBoy]23:21 12 Nov '01  
GeneralRe: from india?memberAmit Dey0:18 15 Oct '01  
GeneralRe: from india?memberNish [BusterBoy]0:23 15 Oct '01  
GeneralNot ADO!!!memberAndrew Peace9:13 12 Oct '01  
GeneralRe: Not ADO!!!memberJörgen Sigvardsson22:29 12 Nov '01  
GeneralRe: Not ADO!!!memberNish [BusterBoy]22:32 12 Nov '01  

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

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