A Closer Look at the Foundation of ADO.NET: The DataSet Object
The DataSet object provides a common way to represent and manipulate data. It's important not to think of a DataSet object as a database but as a cache of data from any source. You use a DataSet not only when querying a database, but anytime you need to move data around. A data set can be created programmatically and filled with data, and then the data can be sent on its way via XML.
A data set is already in XML format, but like light, a data set has a dual nature. Consider that light is composed of both particles and waves simultaneously. A data set can be a binary object, programmable through the .NET classes, while at the same time it can be raw XML. While the DataSet is itself binary, it contains XML and can read and write XML. In fact, it is XML.
To distribute records over the network, you now have an inherently portable solution—use the XML representation of the data set. Target modules will receive XML and can use it as best they can. If we take a peek at the map of the .NET Framework shown in Figure 10-3, we can see that data access and XML are in the same logical block.
Figure 10-3
ADO.NET and XML together in the .NET Framework.
The DataSet object in ADO.NET resides in the System.Data namespace. As its name implies, this namespace is all about data. It contains classes and methods for manipulating data within a DataSet. Table 10-2 lists the key objects in the System.Data namespace.
Object | Description |
DataSet | In-memory container for data. |
DataTable | Used to retrieve the TablesCollection, which holds any DataTable object added to a DataSet. |
DataRow | Used to retrieve the RowsCollection, which holds any DataRow object in a DataTable. |
The DataTable Object
You can easily add a DataTable from a data source such as Microsoft SQL Server 2000 or create your own DataTable within a DataSet. Table 10-3 lists the important properties of the DataTable object.
If you programmatically create a DataTable using a DataSet, you create a DataColumn by providing the column name and data type. Then you add the DataColumn to the DataTable. Using the DataSet.Tables property, the Data Table is added to the DataSet. You repeat adding DataColumn and DataTable objects until you are satisfied with the result. I'll examine this procedure a bit more in the next chapter.
Property | Description |
Columns | Retrieves the columns as a collection. Returns the DataColumnCollection, which contains any or all Column objects. |
Rows | Retrieves the rows in the DataTable by returning a DataRow Collection of DataRow objects. |
ParentRelations | Returns a DataRelationCollection of all logical relationships between the tables. |
Constraints | Returns a ConstraintCollection of the table constraints. |
DataSet | Returns the DataSet to which the DataTable belongs. |
PrimaryKey | Gets or sets the primary key of the table using an array of DataColumn objects. The objects make up the table's primary key. |
The DataSet Object and XML
The DataSet object can use any data type. This openness is accomplished through the use of XML. As I touched on earlier in this chapter, the DataSet is capable of reading and writing its data and schema as XML. Of course, this ability permits you to both create and modify data in a data set using XML or an XML-enabled solution such as SQL Server 2000.
If you've thought you could get along without learning XML, your time is up. ADO.NET, as well as the entire .NET Framework infrastructure, makes extensive use of XML. XML is essentially a plain text representation of data, which means that it can pass through firewalls just like HTML. Unlike with binary data (such as COM or COM+ objects), firewalls can inspect XML data and recognize that it is simply text. Therefore, the data can be passed in and out of port 80 on a server, which is the HTTP port. In fact, you can take XML (which describes the data) and combine it in an HTML envelope (which formats the data) to exchange data between heterogeneous systems on the Internet. This mechanism is what underlies a format known as SOAP, for Simple Object Access Protocol. We will discuss SOAP in Chapter 12, "ASP.NET and XML Web Services." Table 10-4 lists the XML methods for the DataSet object.
Methods | Description |
ReadXML | Reads an XML schema and data into the DataSet. |
ReadXMLSchema | Reads an XML schema into the DataSet. |
WriteXML | Writes an XML file from the DataSet. |
WriteXMLSchema | Writes an XML schema from the DataSet. |
DataView Objects
Using a DataView object, you can create multiple views of any given table. For example, you might have an Employees table with both a department and a skills column. You could create two different views of the data—one for departments, and another for skills. The DataView object is designed so that it can be directly bound to either a Windows form or a Web Form. Table 10-5 lists some important properties of the DataView object.
Property | Description |
Table | Gets or sets the source DataTable for the view. |
Sort | Gets or sets the sort column and the sort order, either descending or ascending. |
RowFilter | Gets or sets the expression used to filter which rows are displayed. |
RowStateFilter | Gets or sets the row state filter, which could include Current Rows, Deleted, ModifiedCurrent, None, ModifiedOriginal, New, OriginalRows, or Unchanged enumerated values. |