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.