Accessing XML-based Data
At the beginning of this section it was mentioned that the DataSet
was designed to abstract data in a way that is independent of
the actual data source. You can see this by changing the focus of your
samples from SQL to XML. The DataSet supports a
ReadXml method that takes a FileStream object as its
parameter. The file you read in this case must contain both a schema
and the data you wish to read. The DataSet expects data to
be in the form, as shown in the following example.
<DocumentElement>
<TableName>
<ColumnName1>column value</ColumnName1>
<ColumnName2>column value</ColumnName2>
<ColumnName3>column value</ColumnName3>
<ColumnName4>column value</ColumnName4>
</TableName>
<TableName>
<ColumnName1>column value</ColumnName1>
<ColumnName2>column value</ColumnName2>
<ColumnName3>column value</ColumnName3>
<ColumnName4>column value</ColumnName4>
</TableName>
</DocumentElement>
Each TableName section corresponds to a single row in the
table. The following sample shows reading schema
and data from an XML file using the ReadXml method of the
DataSet. Note that after the data is read into the DataSet
it is indistinguishable from SQL data -- the DataGrid
binds to it just the same, as shown in the following sample.
You can also read the data and schema separately, using the
ReadXmlData and ReadXmlSchema methods
of the DataSet, as shown in the following sample.
Just as the DataSet supports reader methods for XML data,
it also supports writing the data. The following sample implements a tool for
selecting data from SQL and writing the result as XML data or schema text.
- The common language runtime's managed data access APIs abstract
data and present it in a consistent way regardless of its
actual source (SQL Server, OLEDB, XML, and so on).
- To give your page access to the classes you will need to perform
SQL data access, you must import the System.Data and
System.Data.SqlClient namespaces into your page.
- Populating a dataset from a SQL query involves creating a
SqlConnection, associating a SqlDataAdapter object with
the connection that contains your query statement, and filling the
dataset from the command.
- The DataGrid control supports a DataSource property
that takes any object that implements IList or IListSource interfaces.
You can set this to the result of a SQL query by assigning the
DataSet's DefaultView property, which is of type DataView.
- The SqlDataAdapter maintains a Parameters collection that
can be used to replace variable identifiers (denoted by an "@" in front
of the name) with values.
- When performing commands that do not require data to be returned,
such as inserts, updates, and deletes, you use a SqlCommand
instead of the SqlDataAdapter. The command is issued by calling
an ExecuteNonQuery method, which returns the number of rows affected.
- The SqlConnection must be explicitly opened when you use
the SqlCommand (the SqlDataAdapter automatically handles
opening the connection for you). Always remember to close the
SqlConnection to the data model before the page finishes executing.
If you do not close the connection, you migh inadvertantly exhaust the
connection limit while waiting for the page instances to be released
to garbage collection.
- To allow rows to be edited, the DataGrid supports an
integer EditItemIndex property, which indicates which row of the
grid should be editable. When this property is set, the DataGrid
renders the row at that index as text input boxes instead of simple labels.
- The DataGrid exposes a DataKeyField property that you
can set to the field name for the primary key. In the event handler wired
to the UpdateCommand, you can retrieve the key name from the
DataGrid's DataKeys collection.
- Using BoundColumn controls in the DataGrid gives you
complete control over the order of the columns, as well as their ReadOnly
properties.
- Using TemplateColumn controls in the DataGrid gives you
complete control over the contents of the column.
- The ButtonColumn control can be used to simply render a button
control in each row for that column, which can be associated with an event.
- A HyperLinkColumn can be added to the DataGrid's Columns
collection, which supports navigating to another page when the link is clicked.
- When the DataGrid's AllowSorting property is set to
true, it renders hyperlinks for the column headers that fire a
Sort command back to the grid. You set the OnSortCommand
property of the DataGrid to the handler you want to call when the
user clicks a column link.
- The DataSet supports ReadXml, ReadXmlData, and
ReadXmlSchema methods that take a FileStream as a parameter,
which can be used to populate a DataSet from an XML file.
- Using stored procedures can reduce the cost of performing heavy database
operations in an application.
Copyright 2001-2002 Microsoft Corporation. All rights reserved.