using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Relationship_between_rows
{
class Program
{
static void Main(string[] args)
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Initial Catalog=sangram;Data Source=(Local)");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from course", con);
SqlCommandBuilder sbuilder = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "course");
da = new SqlDataAdapter("select * from student", con);
da.Fill(ds,"student");
//setting relation between cno field common to both
DataRelation dataRel=ds.Relations.Add("RelName",ds.Tables[0].Columns["cno"],ds.Tables[1].Columns["cno"]);
foreach(DataRow courseRow in ds.Tables[0].Rows)
{
Console.WriteLine("cno: " + courseRow["cno"]+ " CName: "+courseRow["cname"]);
foreach(DataRow StudRow in courseRow.GetChildRows(dataRel))
{
Console.WriteLine("Sname: " + StudRow["sname"]);
}
Console.WriteLine();
}
con.Close();
Console.ReadKey();
}
}
}
No comments:
Post a Comment