Mark
Mark

Reputation: 1799

ADO.NET DataSet usage

I am currently trying to use an SQLite database with a DataGridView in C#.

I have seen an example program which does the following:

DataSet combinedDataSet = new DataSet();
DataTable ContactsTable = CombinedDataSet.Tables.Add("contacts");
DataTable FoodTable = CombinedDataSet.Tables.Add("food");

SQLiteDataAdapter ad = new SQLiteDataAdapter(command)

ad.Fill(FoodTable);
ad.Fill(ContactsTable);

This data table is then used as the source for a datagridview.

My Question is this:

Why use a DataSet? Why not just go straight to a DataTable?

I am sure there is a very good reason, but having taken it out of the code, it still appears to run fine.

Upvotes: 0

Views: 942

Answers (4)

John Saunders
John Saunders

Reputation: 161831

A DataSet is an in-memory representation of relational data. In particular, it may contain several DataTables, with relations between them.


See "DataSets, DataTables, and DataViews (ADO.NET)" for a good set of overview articles. You'll see how the DataSet maintains a collection of DataRelation objects, and how each DataTable maintains it's own collection of parent and child relations.

Upvotes: 1

k3b
k3b

Reputation: 14775

It is perfacly ok to have only DataTables without DataSets

Beside what @John Saunders said about relations, i can think of two more benefits

When working with typed datasets (generated from xsl) you usally work on the level of datasets because Typed DataTables are nested inside the DataSet.

The Dataset is useful for debugging. In the inermediate-window you can call combinedDataSet.GetXml() to look into it or save it to a file for better analyse of the content. I am not shure if the new dotnet-4 allow Table.GetXml() but up to dotnet-3.5 table.GetXml() does not exist.

Upvotes: 0

Mark Wilkins
Mark Wilkins

Reputation: 41262

From a practical standpoint, it may be necessary to use DataSet objects if you are using a DataAdapter object to perform updates. For example, if you want to use the DataAdapter.Update method, you have to pass it a DataSet object.

Upvotes: 0

DOK
DOK

Reputation: 32851

I agree that there is not particular reason here for putting the DataTables into a DataSet. The DataSet is just a convenient container for passing the DataTables around, including potentially to Session.

If you think it is simpler and cleaner to call for the DataTables separately, as needed, and not associate them with each other in a DataSet, that will work just fine.

Upvotes: 0

Related Questions