using System;
using System.Data;
using System.Data.SqlClient;
public class XmlRelations
{
private static string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
private static string categorySQL = "SELECT CategoryID, CategoryName,Description FROM Categories";
private static string productSQL = "SELECT ProductID, ProductName, CategoryID FROM Products";
public static void Main()
{
SqlConnection con = new SqlConnection(connectionString);
SqlCommand com = new SqlCommand(categorySQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(com);
DataSet ds = new DataSet("Nortwind");
con.Open();
adapter.FillSchema(ds, SchemaType.Mapped, "Categories");
adapter.Fill(ds, "Categories");
adapter.SelectCommand.CommandText = productSQL;
adapter.FillSchema(ds, SchemaType.Mapped, "Products");
adapter.Fill(ds, "Products");
con.Close();
DataColumn parentCol = ds.Tables["Categories"].Columns["CategoryID"];
DataColumn childCol = ds.Tables["Products"].Columns["CategoryID"];
DataRelation relation = new DataRelation("Cat_Prod", parentCol, childCol);
ds.Relations.Add(relation);
relation.Nested = true;
ds.WriteXml("mydata.xml");
ds.WriteXml(Console.Out);
}
}
|