.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani
Sohaib Khalid

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Typed DataSet

Posted By:Shashi Ray       Posted Date: September 21, 2008    Points: 10    Category: .Net Framework    URL: http://www.dotnetspark.com  



Typed DataSet:


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.


[Visual Basic]

Imports XSDSchema.Namespace


using XSDSchema.Namespace;

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.


[Visual Basic]

Dim custDS As CustomerDataSet= New CustomerDataSet()

Dim custCMD As SqlDataAdapter New SqlDataAdapter("SELECT * FROM Customers", _

                                                 "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=NIC")


custCMD.Fill(custDS, "Customers")


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");


custCMD.Fill(custDS, "Customers");


foreach(CustomerDataSet.CustomersRow custRow in custDS.Customers)


Following is the XML Schema used for the example.
















 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend