working with relational data in ado.netsubmitted byuser leveldate of submissionc.vinodh kumarintermediate04/03/2001
working with hierarchal data was not that easy with the previous version of ado. with the advent of ado.net things are made much easier for the programmers. they need not write complex data shape query to get the hierarchal output (hierarchal recordset抯 in ado).
ado.net dataset
the dataset object is central to ado.net. the dataset is a simple memory-resident database which provides a consistent programming model regardless of the data source. the dataset represents a complete set of data including related tables, constraints, and relationships among the tables. in ado.net, dataset is a collection of one or more tables represented by datatable objects. the table collection object contains all the datatable objects in a dataset.
a typical dataset contains relationships contained by the relationscollection object. a relationship, represented by the datarelation object, associates rows in one table to the rows in another table. it is similar to the foreign-key relationship in a relational database. a datarelation identifies matching columns in two tables of a dataset.
a dataset can contain either related or unrelated data. a dataset can either have two unrelated or related tables. a dataset can be thought of as a document of data. in fact, an xml data document is like this, except it is based on a hierarchical paradigm. because data is often stored in relational databases, the dataset can handle both hierarchical relationships and foreign key relationships. relationships can also have different types of enforcement. by default, deletions and updates are cascaded. a dataset contains a relations collection. it is easy to add a relationship to this collection using the column or columns (in a multi-column key) of the related tables.
the example below presumes two datatable objects exist in the dataset. both tables have a column named "custid" which serves as the link between the two tables. the example adds a single datarelation to the relations collection of the dataset object. the first argument ("custorders") specifies the name of the relationship. the second and third arguments are the datacolumn objects that link the two tables.
//constructs an instance of a dataset<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
dataset ds = new dataset("customerorders");
// add a datarelation to the relations collection specifying its name,
// and the appropriate datacolumn objects as arguments.
ds.relations.add("custorders",ds.tables["customers"].columns["custid"],
ds.tables["orders"].columns["custid"]);
// you can also set the relation like the one given below
datarelation dr;
dr = new datarelation("custorders",ds.tables["customers"].columns["custid"],
ds.tables["orders"].columns["custid"]);
ds.relations.add(dr)
this code adds a relation between the customerid key on the customers table and the customerid foreign key on the orders table in the dataset. now that a relationship exists, you can iterate through the data, using the relationship, in a meaningful way.
retrieving data from the relationship.
foreach (datarow customer in dscustomers.tables["customers"].rows) {
response.write("customer: " + customer["contactname"].tostring());
foreach (datarow order in customer.getchildrows(dscustomers.relations["custorders"])) {
response.write("order #" + order["orderid"].tostring());
}
}
a primary function of the datarelation is to allow navigation from one table to another within the dataset. in practice, this allows us to retrieve all the related datarow objects in one table when given a single datarow from a related table. the above example gives a datarow from the customers table; we can retrieve all the orders for a particular customer from the orders table.
getchildrow method gets the specified child rows of this datarow using the named datarelation.
the relational output from the customer and orders table will be the same as below (one-to-many relation).
customer: maria anders
order #10643
order #10692
order #10702
order #10835
order #10952
order #11011
customer: ana trujillo
order #10308
order #10625
order #10759
order #10926厖厖?
厖厖?
conclusion
in this article, we have started with an introduction to the types of relationships and have learnt how to get the relational data using ado+ dataset and datarelation.in particular ado.net offers several advantages over previous versions of ado and over other data access components. these benefits fall into the following categories: interoperability, maintainability, programmability, and performance.
source code: datarelation.zip