Relations Between DataTables in a DataSet
The
DataSet class
provides a mechanism for specifying relations between tables in a
DataSet. The DataSet class’s
Relations
property contains a RelationsCollection object, which
maintains a collection of DataRelation objects. Each DataRelation
object represents a parent/child relationship between two tables in
the DataSet. For example, there is conceptually a parent/child
relationship between a Customers table and an
Orders table, because each order must belong to
some customer. Modeling this relationship in the DataSet has these
benefits:
The DataSet can enforce relational integrity.
The DataSet can propagate key updates and row deletions.
Data-bound controls can provide a visual representation of the relation.
Example 8-4 loads a Customers
table and an Orders table from the Northwind
database and then creates a relation between them. The statement that
actually creates the relation is shown in bold.
Example 8-4. Creating a DataRelation between DataTables in a DataSet
' Open a database connection. Dim strConnection As String = _ "Data Source=localhost;Initial Catalog=Northwind;" _ & "Integrated Security=True" Dim cn As SqlConnection = New SqlConnection(strConnection) cn.Open( ) ' Set up a data adapter object. Dim strSql As String = "SELECT * FROM Customers" _ & " WHERE City = 'Buenos Aires' AND Country = 'Argentina'" Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, cn) ' Load a data set. Dim ds As DataSet = New DataSet( ) da.Fill(ds, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access