Relations Between DataTables in a DataSet

360 Create a SqlCommand object and assign it to the UpdateCommand property. da.UpdateCommand = New SqlCommandstrUpdateCommand, cn Set up parameters in the SqlCommand object. Dim param As SqlParameter CategoryID param = da.UpdateCommand.Parameters.Add _ New SqlParameterCategoryID, SqlDbType.Int param.SourceColumn = CategoryID param.SourceVersion = DataRowVersion.Original Description param = da.UpdateCommand.Parameters.Add _ New SqlParameterDescription, SqlDbType.NChar, 16 param.SourceColumn = Description param.SourceVersion = DataRowVersion.Current Load a data set. Dim ds As DataSet = New DataSet da.Fillds, Categories Get the table. Dim dt As DataTable = ds.TablesCategories Get a row. Dim row As DataRow = dt.SelectCategoryName = Dairy Products0 Change the value in the Description column. rowDescription = Milk and stuff Perform the update. da.Updateds, Categories Close the database connection. cn.Close

8.6 Relations Between DataTables in a DataSet

The DataSet class provides a mechanism for specifying relations between tables in a DataSet. The DataSet classs Relations property contains a RelationsCollection object, which maintains a collection of DataRelation objects. Each DataRelation object represents a parentchild relationship between two tables in the DataSet. For example, there is conceptually a parentchild 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. Ex am ple 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 361 Dim cn As SqlConnection = New SqlConnectionstrConnection 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 SqlDataAdapterstrSql, cn Load a data set. Dim ds As DataSet = New DataSet da.Fillds, Customers Set up a new data adapter object. strSql = SELECT Orders. _ FROM Customers, Orders _ WHERE Customers.CustomerID = Orders.CustomerID _ AND Customers.City = Buenos Aires _ AND Customers.Country = Argentina da = New SqlDataAdapterstrSql, cn Load the data set. da.Fillds, Orders Close the database connection. cn.Close Create a relation. ds.Relations.AddCustomerOrders, _ ds.TablesCustomers.ColumnsCustomerID, _ ds.TablesOrders.ColumnsCustomerID As shown in Ex am ple 8- 4 , the DataRelationCollection objects Add method creates a new relation between two tables in the DataSet. The Add method is overloaded. The syntax used in Ex am ple 8- 4 is: Public Overloads Overridable Function Add _ ByVal name As String, _ ByVal parentColumn As System.Data.DataColumn, _ ByVal childColumn As System.Data.DataColumn _ As System.Data.DataRelation The parameters are: name The name to give to the new relation. This name can be used later as an index to the RelationsCollection object. parentColumn The DataColumn object representing the parent column. childColumn The DataColumn object representing the child column. 362 The return value is the newly created DataRelation object. Ex am ple 8- 4 ignores the return value.

8.7 The DataSets XML Capabilities