Using the DataSet Class

The dataset acts as a local repository for your data, and it'll be the target of our data operations when we retrieve data from a data provider in a few pages. As you know, you can use the Fill method of a data adapter to fill datasets with data, and if that dataset is bound to controls, those controls will display that new data automatically. You can find the significant public properties of DataSet objects in Table 10.16, their significant methods in Table 10.17, and their significant events in Table 10.18.

Table 10.16. Significant Public Properties of DataSet Objects

PROPERTY

PURPOSE

DataSetName

Returns or sets the dataset name .

HasErrors

Returns True if there are errors in any row in any table.

Relations

A collection of relation objects, which link tables.

Tables

Returns the tables in the dataset.

Table 10.17. Significant Public Methods of DataSet Objects

METHOD

PURPOSE

AcceptChanges

Accepts (that is, commits) changes made to a dataset.

Clear

Clears the dataset by removing all rows in all tables.

Copy

Copies the dataset.

GetChanges

Returns a dataset that contains all changes made to the current dataset.

GetXml

Returns the data in the dataset using XML.

GetXmlSchema

Returns the XSD schema for the dataset.

HasChanges

Indicates whether the dataset has changes that have not yet been committed.

Merge

Merges this dataset with another dataset.

ReadXml

Reads data into a dataset from XML.

ReadXmlSchema

Reads an XML schema into a dataset.

RejectChanges

Rolls back the changes made to the dataset since it was created or since the AcceptChanges method was last called.

Reset

Resets the dataset to the original state.

WriteXml

Writes the dataset's schema and data to XML.

WriteXmlSchema

Writes the dataset schema to XML.

Table 10.18. Significant Public Events of DataSet Objects

EVENT

MEANING

MergeFailed

Happens when a merge operation fails.

Datasets can be typed or untyped usually, they're typed, which means that C# will keep track of the data type of each field, and will object if you try to assign data of the wrong type to a field. ADO.NET uses XML to transport dataset data, and typed datasets hold their type information in XML schemas.

DATASETS AND XML

To see what a dataset looks like in XML, call the dataset WriteXmlSchema method to write out the XML schema for a dataset, or the WriteXml method to write out both the XML schema and the data in the dataset in XML format. You can also use the ReadXml and ReadXmlSchema methods to read data and format information back into a dataset.


Note also that if you've bound a dataset to controls in your application, the user can edit the data that appears in those controls. When the user does so, the data in the dataset is also changedbut not the data in the underlying data store that the data was originally fetched from. You can determine which rows have been changed with the dataset's GetChanges method, and when you use the data adapter's Update method, those changes are sent back to the underlying database in the data provider you're working with. The data provider might make some additional changes, including updating fields that hold calculated values, and return a new dataset. In that case, you can merge those new fields into a dataset using the dataset's Merge method. Then you can use the AcceptChanges method in the dataset to accept the changes or the RejectChanges method to cancel the changes.

It's time for some codinglet's see all this in action. Our first example, ch10_01, creates its own connection, command, and data adapter objects, and uses them to fill a dataset in code.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net