Workshop

for RuBoard

This workshop will help reinforce the concepts covered in today's lesson.

Quiz

1:

Do strongly typed DataSet classes rely on the implementation or interface inheritance as supplied by the common language runtime?

A1:

A strongly typed DataSet class is an example of utilizing the implementation inheritance feature of the common language runtime. This means that the derived class inherits not only the method signatures but also their functionality, and can override that functionality as desired.

2:

What are four ways you can create a strongly typed DataSet ?

A2:

You have the option of designing a strongly typed DataSet from scratch graphically using the designer, by dragging and dropping the database table on the designer from the Server Explorer window, by importing an existing XSD document, or by using the Xml/Schemas DataType Support Utility (XSD.exe).

3:

How does the strongly typed DataSet class expose tables in its DataTableCollection object?

A3:

It exposes them as strongly typed properties that refer to child classes that derive from DataTable .

4:

Why is a DataSet said to be remotable?

A4:

A DataSet is remotable because it's derived from MarshalByValueComponent and is marked with the Serializable attribute. Together these enable a DataSet object to be passed between tiers in a distributed application across AppDomains. In addition, the DataSet implements the ISerializable interface to perform custom serialization using the DiffGram grammar.

Exercise

Q1:

Today, create a strongly typed DataSet that includes the Orders and OrderDetails tables. Then write a method that populates the DataSet using the usp_GetOrders stored procedure.

A1:

First, you must create the strongly typed DataSet by adding a new DataSet to your project and dropping the Orders and OrderDetails onto the designer surface from the Server Explorer. Next, be sure to create the relation between the tables on the OrderId columns because each Order is associated with many OrderDetails rows. Then select Generate DataSet from the menu by right-clicking on the designer surface.

Assuming that the strongly typed DataSet class is called OrdersDs , a method you could possibly use to populate it follows :

 public virtual OrdersDs GetOrdersByCust(Guid customerId) {     SqlConnection con = new SqlConnection(_connect);     SqlDataAdapter da = new SqlDataAdapter("usp_GetOrders",con);     // Create the strongly typed instance     OrdersDs orders = new OrdersDs();     // Setup the call to the stored procedure     da.SelectCommand.CommandType = CommandType.StoredProcedure;     da.SelectCommand.Parameters.Add(        new SqlParameter("@CustomerID",SqlDbType.UniqueIdentifier));     da.SelectCommand.Parameters[0].Value = customerId;     // handle the error     da.TableMappings.Add("Table","Orders");     da.TableMappings.Add("Table1","OrderDetails");     da.MissingSchemaAction = MissingSchemaAction.Error;     // Get the data     try     {         da.Fill(orders);         return orders;     }     catch (SqlException e)     {         // handle the error         return null;     } } 

The GetOrdersByCust method accepts a Guid to represent the CustomerID and then passes it to the usp_GetOrders stored procedure. For the data to map to the appropriate tables in the OrdersDs instance ( orders ), you need to create table mappings for the default tables. In addition, the MissingSchemaAction property is set to Error to ensure that the incoming schema is identical to the schema in the strongly typed DataSet .

for RuBoard


Sams Teach Yourself Ado. Net in 21 Days
Sams Teach Yourself ADO.NET in 21 Days
ISBN: 0672323869
EAN: 2147483647
Year: 2002
Pages: 158
Authors: Dan Fox

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net