SIGN UP MEMBER LOGIN:    
ARTICLE

Create a Strongly Typed DataSet Using The XML Schema Definition Tool (XSD.exe)

Posted by Mukesh Kumar Articles | ADO.NET in C# June 27, 2011
In this article we are going to see how to create a strongly typed DataSet from a XSD schema file using the XML Schema Definition Tool (XSD.exe).
Reader Level:


In this article we are going to see how to create a strongly typed DataSet from a XSD schema file using the XML Schema Definition Tool (XSD.exe).

First, we will create the XSD schema file programmatically.

Follow these steps to create a Typed DataSet :

  1. In Visual Studio .NET 2008/2010, click File > New > Project.
  2. Select your desired programming language and then select the Console Application template.
  3. Replace your Program.cs code with the code given below:

     

    using System;

    using System.Data.SqlClient;

    using System.Data;

     

    namespace DataSetST

    {

    classProgram

        {

    staticvoid Main(string[] args)

            {

    string strFileName = @"..\..\StronglyTypedDataSet.xsd";

     

    //Create Connection

    string strConnectString = "Data Source=(local);Integrated security=SSPI;Initial Catalog=AdventureWorks;";

     

    string strText = "SELECT * FROM Sales.Customer; SELECT * FROM Sales.CustomerAddress;";

     

    // Create and fill a DataSet schema using a data adapter

    SqlDataAdapter objDataAdapter = newSqlDataAdapter(strText, strConnectString);

                objDataAdapter.TableMappings.Add("Table", "Customer");

                objDataAdapter.TableMappings.Add("Table1", "CustomerAddress");

    DataSet objDataSet = newDataSet("StronglyTypedDataSet");

                objDataAdapter.FillSchema(objDataSet, SchemaType.Mapped);

    // Add the data relation

                objDataSet.Relations.Add("Customer_CustomerAddress",

                    objDataSet.Tables["Customer"].Columns["CustomerID"],

                    objDataSet.Tables["CustomerAddress"].Columns["CustomerID"]);

     

    // Write the XSD schema for the DataSet

                objDataSet.WriteXmlSchema(strFileName);

     

            }

        }

    }
    In this code sample we are first creating a connection string to connect to the AdventureWorksdatabase and then we are using a data adapter to create and fill a DataSet Schema and finally writing DataSet Schema to StronglyTypedDataSet.xsd file using WriteXmlSchema() method of DataSet.

    WriteXmlSchema : This method is usedto write the schema for a DataSet to an XML document. The schema includes table, relation, and constraint definitions. The XML schema is written using the XSD standard.
     

  4. Press F5 to create the XSD schema file StronglyTypedDataSet.xsd.

Now the next step is generate a strongly typed DataSet using the XML Schema Definition Tool (XSD.exe).

Follow these steps to generate a strongly typed DataSet:
  1. In Visual Studio .NET 2008/2010, select Visual Studio Tools>Visual Studio Command Prompt.
  2. On command prompt go to the directory containing the XSD schema file.
  3. Use the following command to generate the strongly typed DataSet class file from the XSD schema file:

    xsd StronglyTypedDataSet.xsd /d /l:CS

    DataSet.gif

    The /d switch specifies that source code for a strongly typed DataSet should be created.

    The /l:CS switch specifies that the utility should use the C# language, which is the default if not specified.

    Close the command prompt dialog.
     
  4. The XML Schema Definition Tool (XSD.exe) generates the class file for the strongly typed DataSet and names it using the DataSet name in the XSD schema with the .cs extension.
     
  5. Finally right-click on the project in Solution Explorer and select Add >Existing Item from the context menu and select strongly typed DataSet class to add it to the project. If the strongly typed DataSet file is not visible in the Solution Explorer window, select Show All Files from the Project menu.

The strongly typed DataSet named StronglyTypedDataSet is complete and you can now use it.

Login to add your contents and source code to this article
share this article :
post comment
 

Hello Sir Can you please help me Togenerate the view definition i.e. table-name, field-name, data-type and constraint, if any, have to be saved in XML format. Or Schema Using C#.net

Posted by dhanesh deshmukh May 25, 2012

Thanks Dinesh

Posted by Mukesh Kumar Jun 29, 2011

Good work mukesh..

Posted by Dinesh Beniwal Jun 29, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor