The ADO.NET DataSet class

The DataSet, the heart of disconnected data access, contains two important parts:

– Collection of zero or more tables exposed through the Tables property.

– Collection of zero or more relationships that you can use to link tables together which are exposed through the Relations property.

The next picture presents the basic structure of the DataSet:

 

DataSet – basic structure

DataSet – basic structure

Important notes:

1. The DataSet does not contain all the information from a given table in the data source. You can use the DataSet to work with a small subset of the total information in the data source. In this way performance is improved.

2. The tables in the DataSet do not need to map directly to tables in the data source. A single table can hold the results of a query on one table, or it can hold the results of a JOIN query that combines data from more than one linked table.

3. The data in the data source is not touched at all when you work with the DataSet objects.Instead, all the changes are made locally to the DataSet in memory.

4. The DataSet never retains any type of connection to a data source.

 

Each item in the DataSet.Tables collection is a DataTable. The DataTable contains its own collections—the Columns collection of DataColumn objects (which describe the name and data type of each field) and the Rows collection of DataRow objects (which contain the actual data in each record).

Each record in a DataTable is represented by a DataRow object. Each DataRow object represents a single record in a table that has been retrieved from the data source. The DataRow is the container for the actual field values. You can access them by field name, as in myRow[“FieldName”].

The DataSet also has methods that can write and read XML data and schemas and has methods you can use to quickly clear and duplicate data. The next table describes these methods:

 

Method

Description

GetXml() and

GetXmlSchema()

Returns a string with the data (in XML markup) or schema information for the DataSet. The schema information is the structural information such as the number of tables, their names, their columns, their data types, and their relationships.

WriteXml() and

WriteXmlSchema()

Persists the data and schema represented by the DataSet to a file or a stream in XML format.

ReadXml() and

ReadXmlSchema()

Creates the tables in a DataSet based on an existing XML document or XML schema document. The XML source can be a file or any other stream.

Clear()

Empties all the data from the tables. However, this method leaves the schema and relationship information intact.

Copy()

Returns an exact duplicate of the DataSet, with the same set of tables, relationships, and data.

Clone()

Returns a DataSet with the same structure (tables and relationships) but no data.

Merge()

Takes another DataSet, a DataTable, or a collection of DataRow objects as input and merges them into the current DataSet, adding any new tables and merging any existing tables.