Typed DataSet

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 13.  Programming with ADO.NET


An XML schema can be used to generate a dataset that is "typed." Instead of using the index property of a collection to access an element of the dataset, you can use the name of a column. Here is a fragment from the TypedDataSet example:

 graphics/codeexample.gif Dim UAL As AirlineBroker.AirlinesRow = _    a.FindByName("United") Console.WriteLine("{0}({1}) ReservationNumber:{2} " & _    "WebSite:{3}", UAL.Name.Trim(), _    UAL.Abbreviation.Trim(), _    UAL.ReservationNumber.Trim(), UAL.WebSite.Trim()) 

You can assign a meaningful name to rows as well as use strong typing to make sure you are working with the data element you want. If you try to set the field UAL.ReservationNumber to an integer, the compiler will detect the mistake.

A typed DataSet inherits from the DataSet class so that everything that is available in a DataSet is available in a typed DataSet . If the schema of the database changes, however, the typed dataset class must be regenerated.

The AirlineBroker class that inherits from DataSet is defined in the file AirlineBroker.vb , which is automatically generated from an XML schema, as discussed in the next section. This file is created in the XSD Tool directory, and it is also included in the TypedDataSet project.

 graphics/codeexample.gif '--------------------------------------------------------- ' <autogenerated> '     This code was generated by a tool. ... ' </autogenerated> '--------------------------------------------------------- ... Public Class AirlineBroker     Inherits DataSet     Private tableAirlines As AirlinesDataTable     Private tableFlights As FlightsDataTable ... 

Generating Typed DataSets

The XML Schema Definition Tool ( Xsd.exe ) is used to transform an XML schema (XSD) to a typed dataset. The syntax for doing this is

 Xsd.exe /d /l:vb filename.xsd 

The /d switch indicates that a DataSet should be generated. The /l switch indicates the language. In this case, a VB.NET class will be generated.

The XSD Tool directory has a batch file xmlbuild.bat that can be used to take the revised AirlineBroker XSD and generate a typed dataset AirlineBroker.vb .

Fetching Data with a Typed DataSet

The TypedDataSet example shows how to use a typed dataset to access the Airline Brokers database. You define your SqlConnection as usual and create a SqlDataAdapter instance for each table you want to use. You create whatever SqlCommands you need to work with the data. A typed DataSet is independent of a database, just like the untyped DataSet , so it needs SqlDataAdapter to handle the database operations.

 graphics/codeexample.gif Dim connectString As String = _    "server=localhost;uid=sa;pwd=;database=AirlineBroker" Dim conn As New SqlConnection(connectString) Dim airlinesAdapter As New SqlDataAdapter() Dim flightsAdapter As New SqlDataAdapter() Dim planetypeAdapter As New SqlDataAdapter() Dim customersAdapter As New SqlDataAdapter() Dim reservationsAdapter As New SqlDataAdapter() Dim airlineBrokerDataset As New AirlineBroker() 

Next, the select commands are defined to fetch the data, just as for use with a regular DataSet . For illustrative purposes, constraint checking is enabled even though it is on by default.

 airlinesAdapter.SelectCommand = New SqlCommand(_    "select * from Airlines", conn) airlinesAdapter.InsertCommand = New SqlCommand(_    "insert Airlines(Name, Abbreviation, WebSite, " & _    "ReservationNumber) values(@Name, @Abbrev, @Web, " _    & "@Reserve)", conn) airlinesAdapter.InsertCommand.CommandType = _    CommandType.Text Dim param As New SqlParameter("@Name", SqlDbType.NChar, 40) airlinesAdapter.InsertCommand.Parameters.Add(param) airlinesAdapter.InsertCommand.Parameters(_    "@Name").SourceColumn = "Name" ...   airlineBrokerDataset.EnforceConstraints = true; ... 

Now you can fetch the data. The order in which you do this is important. If Flights data is fetched before PlaneType data, a constraint violation exception will occur, because the PlaneType field in the Flights table does not exist.

 airlinesAdapter.Fill(airlineBrokerDataset, "Airlines") planetypeAdapter.Fill(airlineBrokerDataset, "PlaneType") flightsAdapter.Fill(airlineBrokerDataset, "Flights") customersAdapter.Fill(airlineBrokerDataset, "Customers") reservationsAdapter.Fill(airlineBrokerDataset, _    "Reservations") 

Displaying Data with a Typed DataSet

The strong typing makes it straightforward to display the data:

 Dim a As AirlineBroker.AirlinesDataTable = _    airlineBrokerDataset.Airlines Console.WriteLine(a.TableName) Console.WriteLine("{0, -18} {1, -20} {2, -20} {3, -15}", _    "Name", "Abbreviation", "Web Site", _    "Reservation Numbers") Dim i As Integer For i = 0 To a.Count - 1    Console.WriteLine("{0,-18} {1,-20} {2,-20} {3,-15}", _       a(i).Name.Trim(), a(i).Abbreviation.Trim(), _       a(i).WebSite.Trim(), a(i).ReservationNumber.Trim()) Next ... 

Modify Data with a Typed DataSet

You modify and update the database with a typed dataset just as you do with a regular dataset. Make sure the correct table is specified in the Update method.

 airlineBrokerDataset.Airlines.AddAirlinesRow(_    "Southwest", "S", "1-800-111-222", "www.southwest.com") NumberRows = airlinesAdapter.Update(_    airlineBrokerDataset, "Airlines") If NumberRows = 1 Then    Console.WriteLine("Southwest added.") Else    Console.WriteLine("Southwest not added") End If 

Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

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