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




Returns or sets the dataset name .


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


A collection of relation objects, which link tables.


Returns the tables in the dataset.

Table 10.17. Significant Public Methods of DataSet Objects




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


Clears the dataset by removing all rows in all tables.


Copies the dataset.


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


Returns the data in the dataset using XML.


Returns the XSD schema for the dataset.


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


Merges this dataset with another dataset.


Reads data into a dataset from XML.


Reads an XML schema into a dataset.


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


Resets the dataset to the original state.


Writes the dataset's schema and data to XML.


Writes the dataset schema to XML.

Table 10.18. Significant Public Events of DataSet Objects




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.


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 © 2008-2017.
If you may any questions please contact us: