Tải bản đầy đủ (.pdf) (16 trang)

Bringing Related Data Together

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (469 KB, 16 trang )

73
Chapter 5
Bringing Related Data Together
After completing this chapter, you will be able to:

Join multiple DataTable instances into a DataSet

Establish parent-child relationships between tables of data

Understand the types of table constraints available in ADO.NET

Build relationships that auto-correct linked rows when needed
The DataTable class provides ADO.NET’s core data-management functionality. But many
of the tools that build and interact with DataTable content do so through a higher level of
abstraction: the DataSet. Instead of relying on a single table’s worth of DataRows, a DataSet
links multiple tables together, making it possible to generate data queries based on the rela-
tionships between the tables and their data.
In this chapter, the DataSet object takes center stage. You will discover how a DataSet becomes
more than the sum of its DataTable parts. By combining data tables, relationship definitions be-
tween those tables, and column-specific constraints that help ensure data integrity between
the tables, ADO.NET provides new views on data that would be complicated to achieve with
solitary data tables.
Note
The exercises in this chapter all use the same sample project, a tool that shows the related
records between two DataTable instances. Although you will be able to run the application after
each exercise, the expected results for the full application might not appear until you complete
all exercises in the chapter.
Collecting Tables into Sets
ADO.NET includes a System.Data.DataSet class that defines a collection of tables, their rela-
tionships, and related field constraints. To establish a data set in your program, create a new
DataSet object, optionally passing it a set name.


C#
DataSet someSet = new DataSet("SetName");
Visual Basic
Dim someSet As New DataSet("SetName")
Dwonloaded from: iDATA.ws
74
Microsoft ADO.NET 4 Step by Step
Adding a name to a standalone DataTable instance might be inconsequential, but some
table-related features in ADO.NET do enable access to a DataTable object by its table name.
For example, the DataSet class includes a Tables property that, as expected, holds a collection
of individual DataTable instances. You access tables within the collection either by name or
by an index number. To add a new DataTable to a DataSet, write the following:
C#
someSet.Tables.Add(someTable);
Visual Basic
someSet.Tables.Add(someTable)
You can also pass a string to the Add method, which creates a new named table object with-
out columns or rows. You can add as many data tables as you want to the Tables collection.
At this point, they are still treated as individual tables; adding them to the collection of tables
does not automatically endow them with relationship features.
Note
A DataSet can contain two tables with the same name as long as their namespace values
differ. Chapter 7, “Saving and Restoring Data,” discusses these namespaces. Also, if two tables
share a common name (and namespace) but differ in the casing of those names (“CUSTOMERS”
versus “customers”), the DataSet will treat them as distinct tables. When querying these tables,
you must provide the same casing as the original table names, or else the query will fail.
However, if a table name has no duplicate within a DataSet, its name in queries can be case-
insensitive.
The DataSet includes some properties and methods that replicate the functionality of the
contained tables. These features share identical names with their table counterparts. When

used, these properties and methods work as if those same features had been used at the
table level in all contained tables. Some of these members that you’ve seen before include
the following:

Clear

CaseSensitive

AcceptChanges

RejectChanges

EnforceConstraints

HasErrors
Dwonloaded from: iDATA.ws
Chapter 5 Bringing Related Data Together
75
Adding Tables to a
DataSet
: C#
1. Open the “Chapter 5 CSharp” project from the installed samples folder. The project in-
cludes three Windows.Forms classes: FlightInfo, FlightDetail, and LegDetail.
2. Open the source code view for the FlightInfo form. Locate the BuildSampleDataSet
function. This routine creates the main DataSet used in the application.
3. Just after the “Add the two tables to the data set” comment, add the following
statements:
result = new DataSet("FlightSample");
parentTable = BuildFlightTable();
childTable = BuildLegTable();

result.Tables.Add(parentTable);
result.Tables.Add(childTable);
These lines create two tables that share a common value: the flight ID number. In the
flight table the field is named ID, whereas it is called FlightID in the leg table. A later
example in this chapter will establish the relationship between the two tables.
Adding Tables to a
DataSet
: Visual Basic
1. Open the “Chapter 5 VB” project from the installed samples folder. The project includes
three Windows.Forms classes: FlightInfo, FlightDetail, and LegDetail.
2. Open the source code view for the FlightInfo form. Locate the BuildSampleDataSet
function. This routine creates the main DataSet used in the application.
3. Just after the “Add the two tables to the data set” comment, add the following
statements:
result = New DataSet("FlightSample")
parentTable = BuildFlightTable()
childTable = BuildLegTable()
result.Tables.Add(parentTable)
result.Tables.Add(childTable)
These lines create two tables that share a common value: the flight ID number. In the
flight table the field is named ID, whereas it is called FlightID in the leg table. A later
example in this chapter will establish the relationship between the two tables.
Dwonloaded from: iDATA.ws
76
Microsoft ADO.NET 4 Step by Step
Establishing Relationships Between Tables
Before focusing on the relationship features of the DataSet class, it is essential to have a clear
understanding of what it means for two tables to be related.
Understanding Table Relations
In relational database modeling, the term cardinality describes the type of relationship that

two tables have. There are three main types of database model cardinality:

One-to-One A record in one table matches exactly one record in another table. This
is commonly used to break a table with a large number of columns into two distinct
tables for processing convenience.
Table1
Record 1
Record 2
Record 3
Table2
Record 1
Record 2
Record 3

One-to-Many One record in a “parent” table has zero or more “child” records in
another table. A typical use for the one-to-many relationship is in an ordering system
in which a single customer record (the parent) will have none, one, or many order re-
cords (the children) on file. Likewise, a single order record will have multiple order line
items. One-to-many relationships are the most common type of table link in relational
databases.
Customer
Customer 1
Customer 2
Customer 3
Order
Order 1 for Customer 1
Order 2 for Customer 1
Order 1 for Customer 2

Many-to-Many In this third and most complex type of link, one record in the first

table is associated with zero or more records in the second table, and each record in
the second table can also be associated with zero or more records in the first table.
Students taking classes is a typical real-world example of a many-to-many relationship.
Each student can take multiple classes, and each class can have multiple students listed
as class participants.
Dwonloaded from: iDATA.ws
Chapter 5 Bringing Related Data Together
77
Student
Student 1
Student 2
Student 3
Class
Class 1
Class 2
Class 3
Fortunately, all three of these model relationships share a common physical implementation
concept: the foreign key—the use of a table’s identifying column(s) by another table. In a
foreign-key relationship, both tables include a column (or multiple columns when the master
table uses a multipart key) that uses an agreed-upon domain of values. When records in each
table share a common value for that column, the records are related.
For example, in a customer-order relationship, the Customer table includes a customer iden-
tification column that uniquely defines each record. The associated Order table also includes
a customer identification column. Each order that shares a customer identifier with a specific
customer record belongs to that customer record. There might be multiple order records
that include that customer’s identifier, each of which is related to the same customer.
Note
The name given to the identifying column in the first table doesn’t need to be the same
as the name of the column in the second table. Only the data relationships are important, not the
names given to the columns in those relationships.

Records in a one-to-one relationship work the same way, but there is never more than one
occurrence of a specific identifier value in each table. If a record in one table always has a
match in the second table, it doesn’t matter which one is the parent and which is the child. If
one table’s records are optional, the table with the optional records is the child.
Many-to-many relationships also use the foreign-key concept, but they require a “go-between”
table that indicates which two keys link up. Both primary tables are the parent; the interim
table is the child.
Student
Student 1
Student 2
Student 3
StudentClassInterim
Student 1
Student 1
Student 1
Class 1
Class 2
Class 3
Student 3 Class 2
Student 3 Class 3
Class
Class 1
Class 2
Class 3
Dwonloaded from: iDATA.ws
78
Microsoft ADO.NET 4 Step by Step
There are some expectations that come with these types of data relationships:

The relationship column in the parent or master table must contain unique values; no

duplicates are allowed. Also, NULL values are not allowed in this column.

Any value that appears in the relationship column of the child must have a related par-
ent record. If the child record has no related parent record, that child record must be
deleted or its link-column value must be set to NULL.
In short, every parent must be unique, and every child requires a parent.
Creating Data Relations
The DataRelation class, found within the System.Data namespace, makes table joins within a
DataSet possible. Each relationship includes a parent and a child. The DataRelation class even
uses the terms “parent” and “child” in its defining members.
To create a relationship between two DataSet tables, first add the parent and child table to
the data set. Then create a new DataRelation instance, passing its constructor the name of
the new relationship, plus a reference to the linking columns in each table. The following
code joins a Customer table with an Order table, linking the Customer.ID column as the par-
ent with the related Order.CustomerID column as the child:
C#
DataSet orderTracking = new DataSet("OrderTracking");
orderTracking.Tables.Add(customerTable);
orderTracking.Tables.Add(orderTable);
DataRelation customerOrder = new DataRelation("CustomerOrder",
customerTable.Columns["ID"], orderTable.Columns["CustomerID"]);
orderTracking.Relations.Add(customerOrder);
Visual Basic
Dim orderTracking As New DataSet("OrderTracking")
orderTracking.Tables.Add(customerTable)
orderTracking.Tables.Add(orderTable)
Dim customerOrder As New DataRelation("CustomerOrder",
customerTable.Columns!ID, orderTable.Columns!CustomerID)
orderTracking.Relations.Add(customerOrder)
For tables with multipart relational keys, the second and third arguments to the DataRelation

constructor each accept an array of DataColumn objects.
Note
Columns bound in a relationship must always be the same data type. In tables with multi-
part keys, each positional part between the tables must be the same data type.
Dwonloaded from: iDATA.ws

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×