The DataSet Classes Unlike the data provider classes, only one set of DataSet classes exists. The DataSet classes are all contained in the System.Data namespace, and they represent data in an abstract form that's not tied to any particular database implementation. This section introduces you to the DataSet class and the other objects that it contains, including the following: DataTable DataRelation DataRow DataColumn DataView The DataSet class itself is a self-contained, memory-resident representation of relational data. A DataSet object contains other objects, such as DataTable and DataRelation objects, that hold the actual data and information about the design of the data. Table 2.5 shows the most important members of the DataSet class. Table 2.5. Important Members of the DataSet ClassMember | Type | Description |
---|
AcceptChanges() | Method | Marks all changes in the dataset as having been accepted | Clear() | Method | Removes all data from the dataset | GetChanges() | Method | Gets a DataSet object that contains only the changed data in the dataset | GetXml() | Method | Gets an XML representation of the dataset | GetXmlSchema() | Method | Gets an XML Schema Definition (XSD) representation of the dataset schema | Merge() | Method | Merges two datasets | ReadXml() | Method | Loads the dataset from an XML file | ReadXmlSchema() | Method | Loads the dataset schema from an XSD file | Relations | Property | Specifies a collection of DataRelation objects | Tables | Property | Specifies a collection of DataTable objects | WriteXml() | Method | Writes the dataset to an XML file | WriteXmlSchema() | Method | Writes the dataset schema to an XSD file | The DataTable Class The DataTable class represents a single table within the DataSet object. A single DataSet object can contain many DataTable objects. Table 2.6 shows the most important members of the DataTable class. Table 2.6. Important Members of the DataTable ClassMember | Type | Description |
---|
ChildRelations | Property | Specifies a collection of DataRelation objects that refer to children of the DataTable class | Clear() | Method | Removes all data from the DataTable class | ColumnChanged | Event | Occurs when the data in any row of a specified column has been changed | ColumnChanging | Event | Occurs when the data in any row of a specified column is about to be changed | Columns | Property | Specifies a collection of DataColumn objects | Constraints | Property | Specifies a collection of Constraint objects | DataSet | Property | Retrieves the DataSet object to which the DataTable object belongs | NewRow() | Method | Creates a new, blank row in the data table | ParentRelations | Property | Specifies a collection of DataRelation objects that refer to parents of the DataTable object | PrimaryKey | Property | Specifies an array of DataColumn objects that provide the primary key for the DataTable object | RowChanged | Event | Occurs when any data in a DataRow object has been changed | RowChanging | Event | Occurs when any data in a DataRow object is about to be changed | RowDeleted | Event | Occurs when a row has been deleted | RowDeleting | Event | Occurs when a row is about to be deleted | Rows | Property | Specifies a collection of DataRow objects | Select() | Method | Selects an array of DataRow objects that meet specified criteria | TableName | Property | Specifies the name of the DataTable object | As you can see, you can manipulate a DataTable as either a collection of DataColumn objects or a collection of DataRow objects. The DataTable class also provides events that you can use to monitor data changes. For example, you might bind a DataTable to a DataGrid control and use these events to track the user's operations on the data in the DataGrid control. The DataRelation Class The DataRelation object stores information on the relationships between DataTable objects in a DataSet object. The most important members of the DataRelation class are listed in Table 2.7. Table 2.7. Important Members of the DataRelation ClassMember | Type | Description |
---|
ChildColumns | Property | Specifies a collection of DataColumn objects that defines the foreign key side of the relationship | ChildKeyConstraint | Property | Returns a ForeignKeyConstraint object for the relationship | ChildTable | Property | Specifies a DataTable object from the foreign key side of the relationship | DataSet | Property | Retrieves the DataSet object to which the DataRelation object belongs | ParentColumns | Property | Specifies a collection of DataColumn objects that defines the primary key side of the relationship | ParentKeyConstraint | Property | Returns a PrimaryKeyConstraint object for the relationship | ParentTable | Property | Specifies a DataTable object from the primary key side of the relationship | RelationName | Property | Specifies the name of the DataRelation object | The DataRow Class A DataRow object represents a single row of data. When you're selecting, inserting, updating, or deleting data in a DataSet object, you're normally working with DataRow objects. Table 2.8 shows the most important members of the DataRow class. Table 2.8. Important Members of the DataRow ClassMember | Type | Description |
---|
BeginEdit() | Method | Starts editing the DataRow object | CancelEdit() | Method | Discards an edit in progress | Delete() | Method | Deletes the DataRow object from its parent DataTable object | EndEdit() | Method | Ends an edit in progress, saving the changes | IsNull() | Method | Returns true if a specified column contains a null value | ItemArray | Property | Allows the manipulation of a DataRow object through an array of values | RowState | Property | Returns information on the current state of a DataRow object (for example, whether it has been changed since it was last saved to the database) | Table | Property | Retrieves the DataTable object to which the DataRow object belongs | The DataColumn Class A DataColumn object represents a single column in a DataTable object. By manipulating the DataColumn objects, you can determine and even change the structure of the DataTable object. Table 2.9 lists the most important members of the DataColumn class. Table 2.9. Important Members of the DataColumn ClassMember | Type | Description |
---|
AllowDbNull | Property | Indicates whether the DataColumn object can contain null values | AutoIncrement | Property | Indicates whether the DataColumn object is an identity column | ColumnName | Property | Specifies the name of the DataColumn object | DataType | Property | Specifies the data type of the DataColumn object | DefaultValue | Property | Specifies the default value of this DataColumn object for new rows of data | Expression | Property | Specifies an expression that can be used to filter rows, calculate the values in a column, or create an aggregate column | MaxLength | Property | Specifies the maximum length of a text DataColumn object | Unique | Property | Indicates whether values in the DataColumn object must be unique across all rows in the DataTable object | The DataView Class The DataView object represents a view of the data contained in a DataTable object. A DataView object might contain every DataRow object from the DataTable object, or it might be filtered to contain only specific rows. That filtering can be done by SQL expressions or by row state (for example, returning only rows that have been modified). The most important members of the DataView class are detailed in Table 2.10. Table 2.10. Important Members of the DataView ClassMember | Type | Description |
---|
AddNew() | Method | Adds a new row to the DataView object | AllowDelete | Property | Indicates whether deletions can be performed | AllowEdit | Property | Indicates whether updates can be performed | AllowNew | Property | Indicates whether insertions can be performed | Count | Property | Returns the number of rows in this DataView object | Delete() | Method | Deletes a row from this DataView object | Find() | Method | Searches for a row in the DataView object | FindRows() | Method | Returns an array of rows matching a filter expression | RowFilter | Property | A string expression that specifies how the rows are filtered | RowStateFilter | Property | One of the values from the DataViewRowState enumeration that specifies how the rows are filtered | Sort | Property | Specifies the sort column(s) and sort order(s) for this DataView object | |