[ Team LiB ] Previous Section Next Section

22.1 Comments/Troubleshooting

The DataSet class provides a consistent programming model, regardless of the actual data source. The DataSet schema can be created programmatically, read from a data source, read from an XML schema, or inferred from an XML document or stream.

The DataSet serves as a container for disconnected objects. It can contain DataTable objects in its DataTableCollection and can be accessed through its Tables property. These tables can be related to one another using DataRelation objects in the DataRelationCollection and are accessed through the Relations property of the DataSet. Data integrity can be maintained by adding ForeignKeyConstraint and UniqueConstraint objects to the DataTable objects.

The data in the DataSet can be populated from the data source, modified, and later reconciled back to a data source, using a DataAdapter. Alternatively, the data can be populated from XML documents or streams, modified, and saved as an XML document or stream. Finally, the data stored in the DataSet can be created and manipulated programmatically.

The DataSet and XML are tightly coupled, which permits the same data to be accessed and manipulated using either the DataSet and its contained objects or by using XML-based classes. This has two particularly important implications. First, XSLT templates can easily be applied to any DataSet to transform the data structure. Second, the schema, data, or both the schema and data within a DataSet can be persisted to memory or a file as an XML document.

DataSets exist as both untyped and strongly typed. Strongly typed DataSets are a collection of automatically generated classes that inherit from the DataSet, DataTable, and DataRow classes and provide additional properties, methods, and events based on the DataSet schema. Strongly typed DataSets are discussed in detail in Chapter 13.

The commonly used public properties of the DataSet class are listed in Table 22-1.

Table 22-1. DataSet properties

Property

Description

CaseSensitive

Gets or sets a value indicating whether string comparisons in the DataSet are treated as case-sensitive.

Locale

Gets or sets the locale information that is the basis for string comparisons in tables.

DataSetName

Gets or sets the name of the DataSet.

EnforceConstraints

Gets or sets a value indicating whether the constraint rules defined between tables in the DataSet are enforced when data is updated.

HasErrors

Gets a value indicating whether there are any errors in any rows within any of the tables within the DataSet.

Namespace

Gets or sets the namespace of the DataSet.

Prefix

Gets or sets an XML prefix that aliases the namespace of the DataSet.

DefaultViewManager

Gets a custom view of the data contained within the DataSet for filtering, sorting, and navigating using a DataViewManager.

The commonly used public collections of the DataSet class are listed in Table 22-2.

Table 22-2. DataSet collections

Collection

Description

Tables

Accesses the DataTableCollection that contains the tables for the DataSet as a collection of DataTable objects.

Relations

Accesses the DataRelationCollection that contains table relationships for the DataSet as a collection of DataRelation objects.

ExtendedProperties

Accesses the PropertyCollection that contains custom information for the DataSet as a collection of key-and-value pairs.

The commonly used public methods of the DataSet class are listed in Table 22-3.

Table 22-3. DataSet methods

Method

Description

AcceptChanges(  )

Commits all changes made to the DataSet since the last time it was loaded or since the last time changes were committed.

Clear(  )

Removes all data rows from the DataSet.

Clone(  )

Creates a new DataSet with the same schema as the original but with none of the data.

Copy(  )

Creates a new DataSet with the same schema and data as the original.

GetChanges(  )

Gets a copy of the changes made to a DataSet since the last time it was loaded or since changes were last committed.

GetXml(  )

Returns a string that is the XML representation of the data stored in the DataSet, optionally with XSD schema information.

GetXmlSchema(  )

Returns a string that is the XSD schema for the XML representation of the data stored in the DataSet.

HasChanges(  )

Returns a value indicating whether data in the DataSet has been modified, inserted, or deleted since it was last loaded or since changes were last committed.

InferXmlSchema(  )

Infers an XML schema from the contents of a specified Stream, file, or TextReader into the DataSet.

Merge(  )

Merges data from another DataSet, DataTable, or array of DataRows into the DataSet.

ReadXml(  )

Reads XML schema information and data from a Stream, file, TextReader, or XmlReader into the DataSet.

ReadXmlSchema(  )

Reads XML schema information from a Stream, file, TextReader, or XmlReader into the DataSet.

RejectChanges(  )

Rejects all changes made to the DataSet since the last time it was loaded or since the last time changes were committed.

Reset(  )

Discards the contents of the DataSet, resetting it to an uninitialized state.

WriteXml(  )

Writes the data, and optionally the schema, from the DataSet to a Stream, file, TextReader, or XmlReader.

WriteXmlSchema(  )

Writes the schema from the DataSet to a Stream, file, TextReader, or XmlReader.

The commonly used public events of the DataSet class are listed in Table 22-4.

Table 22-4. DataSet event

Event

Description

MergeFailed

Raised when the schema of the source DataSet and target DataSet being merged are in conflict.

The DataSet class is contained within the System.Data namespace. The DataSet class inherits from MarshalByValueComponent and implements the IListSource, ISupportInitialize, and ISerializable interfaces. It is safe for multithreaded read operations; multithreaded write operations must be synchronized. DataSet objects can be passed between different application domains.

    [ Team LiB ] Previous Section Next Section