Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

Using the Typed DataSet

The XML Schema Designer generated a source file that contains a single DataSet. The DataSet contains three nested DataTables, DataRows, and DataRelations that capture the Schema as we defined it visually in the Designer. It is important to note that we get the benefit of strongly typed code, automatically created DataRelation objects—instead of having to define them ourselves—but the generated code is still comprised of ADO.NET classes. As a result, we use the same kind of code to initialize the typed DataSet as we would an untyped DataSet. Listing 1 demonstrates code that initializes our typed DataSet and binds the results to a DataGrid.

Listing 1: Initializing and Using a Typed DataSet.

1: private void Form1_Load(object sender, System.EventArgs e)
2: {
3:  SqlConnection connection = 
4:   new SqlConnection(
5:   "data source=PTK800;initial catalog=Northwind;" + 
6:   "integrated security=SSPI;persist security info=True;" + 
7:   "workstation id=PTK800;packet size=4096");
9:  SqlDataAdapter adapterOrders = new SqlDataAdapter(
10:   "SELECT * FROM ORDERS", connection);
12:  SqlDataAdapter adapterOrderDetails = new SqlDataAdapter(
13:   "SELECT * FROM [ORDER DETAILS]", connection);
15:  SqlDataAdapter adapterProducts = new SqlDataAdapter(
16:   "SELECT * FROM PRODUCTS", connection);
18:  OrderDetails orderDetails = new OrderDetails();
20:  try
21:  { 
22:   adapterOrders.Fill(orderDetails, "Orders");
23:   adapterOrderDetails.Fill(orderDetails, "Order Details");
24:   adapterProducts.Fill(orderDetails, "Products");
25:  }
26:  catch(Exception x)
27:  {
28:   MessageBox.Show(x.Message);
29:  }
31:  dataGrid1.DataSource = orderDetails.Orders;
33: }

The line numbers were added for reference only. The code in Listing 1 is based on a simple Windows Forms application with a single DataGrid. As you can see from the code, initializing a typed DataSet is consistent with initializing an untyped DataSet, with one exception. The example code will have a DataRelation object for each of the relationships: Orders and Order Details and Order Details and Products. We don't have to create these explicitly; the code is defined in the typed DataSet code. (I didn't provide the listing for the typed DataSet because it is about 1700 lines long.)

There are a few technical details to point out here. The first is that OrderDetails is still a DataSet. The only way a Dataset will know that you are initializing the typed DataTable is if you use the table name when you fill the DataSet. If you don't use the table name, the table will be added as an extra table in the DataSet.

The initialization code is not substantially easier. It is after the DataSet is initialized that we begin to reap the benefits of a typed Dataset. For instance, notice that I can assign a specific DataTable to the DataSource. Using an untyped DataSet, line 31 might be rewritten as dataGrid1.DataSource = orderDetails.Tables[0]. Clearly, line 31 refers to the Orders table. You get the same kind of clarity when you begin using tables and rows, too. For example, if I wanted to refer to a specific order's OrderDate, I could write orderDetails.Orders[0].OrderDate. There is no obvious indication that we are even working with a database here, just orders.

  • + Share This
  • 🔖 Save To Your Account