Along with late bound access to values through weakly typed variables, the DataSet provides access to data through a strongly typed metaphor. Tables and columns that are part of the DataSet can be accessed using user-friendly names and strongly typed variables.
A typed DataSet is a class that derives from a DataSet. As such, it inherits all the methods, events, and properties of a DataSet. Additionally, a typed DataSet provides strongly typed methods, events, and properties. This means you can access tables and columns by name, instead of using collection-based methods. Aside from the improved readability of the code, a typed DataSet also allows the Visual Studio .NET code editor to automatically complete lines as you type.
Additionally, the strongly typed DataSet provides access to values as the correct type at compile time. With a strongly typed DataSet, type mismatch errors are caught when the code is compiled rather than at run time.
Strongly Typed DataSet
Given an XML Schema that complies with the XML Schema definition language (XSD) standard, you can generate a strongly typed DataSet using the XSD.exe tool provided with the .NET Framework SDK.
The following code shows the syntax for generating a DataSet using this tool.
xsd.exe /d /l:CS XSDSchemaFileName.xsd /n:XSDSchema.Namespace
In this syntax, the /d directive tells the tool to generate a DataSet, and the /l: tells the tool what language to use (for example, C# or Visual Basic .NET). The optional /n: directive tells the tool to also generate a namespace for the DataSet called XSDSchema.Namespace. The output of the command is XSDSchemaFileName.cs, which can be compiled and used in an ADO.NET application. The generated code can be compiled as a library or a module.
The following code shows the syntax for compiling the generated code as a library using the C# compiler (csc.exe).
csc.exe /t:library XSDSchemaFileName.cs /r:System.dll /r:System.Data.dll
The /t: directive tells the tool to compile to a library, and the /r: directives specify dependent libraries required to compile. The output of the command is XSDSchemaFileName.dll, which can be passed to the compiler when compiling an ADO.NET application with the /r: directive.
The following code shows the syntax for accessing the namespace passed to XSD.exe in an ADO.NET application.
The following code example uses a typed DataSet named CustomerDataSet to load a list of customers from the Northwind database. Once the data is loaded using the Fill method, the example loops through each customer in the Customers table using the typed CustomersRow (DataRow) object. This provides direct access to the CustomerID column, as opposed to accessing it through the DataColumnCollection.
Dim custDS As CustomerDataSet= New CustomerDataSet()
Dim custCMD As SqlDataAdapter New SqlDataAdapter("SELECT * FROM Customers", _
"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=NIC")
Dim custRow As CustomerDataSet.CustomersRow
For Each custRow In custDS.Customers
CustomerDataSet custDS = new CustomerDataSet();
SqlDataAdapter custCMD = new SqlDataAdapter("SELECT * FROM Customers",
"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=NIC");
foreach(CustomerDataSet.CustomersRow custRow in custDS.Customers)
Following is the XML Schema used for the example.