>DataSet1.Relations.Add("Relation1", DataSet1.Tables(0).Columns("Column1"), DataSet1.Tables(1).Columns("Column1"), False) >>Once Relation1 is created, I need to reference that field with the same name in both tables:
>Relation1.Address >>Which Address will return ?
using System; using System.Data; using System.Data.OleDb; namespace relationsample { class Program { static void Main(string[] args) { string strCon = "Provider=VFPOLEDB;Data Source=" + @"C:\PROGRAM FILES\MICROSOFT VISUAL FOXPRO 9\SAMPLES\Data\testdata.dbc"; string strParentSelect = "select cust_id, company as name from customer"; string strChildSelect = "select c.cust_id," + " e.First_Name-e.Last_Name as name," + " o.order_net as orderTotal" + " from customer c" + " left join orders o on o.cust_id = c.cust_id" + " left join employee e on o.emp_id = e.emp_id"; // represents a sample child table OleDbConnection cn = new OleDbConnection(strCon); OleDbDataAdapter daLeft = new OleDbDataAdapter(strParentSelect, cn); OleDbDataAdapter daRight = new OleDbDataAdapter(strChildSelect, cn); DataSet ds = new DataSet("mySample"); daLeft.Fill(ds, "myParent"); daRight.Fill(ds, "myChild"); ds.Relations.Add("p2c", ds.Tables["myParent"].Columns["cust_id"], ds.Tables["myChild"].Columns["cust_id"], false); // add calculated columns to parent // that sums its children and order count ds.Tables["myParent"].Columns.Add("orderSum", typeof(double), "Sum(Child(p2c).orderTotal)"); ds.Tables["myParent"].Columns.Add("orderCount", typeof(int), "Count(Child(p2c).orderTotal)"); // add calculated column to child // that shows its parent's name ds.Tables["myChild"].Columns.Add("myParentName", typeof(string), "Parent(p2c).Name"); foreach (DataRow row in ds.Tables["myParent"].Rows) { Console.WriteLine("Parent:{0}\t\tTotal {1} in {2} orders", row["name"], row["orderSum"], row["orderCount"]); Console.WriteLine("\tDetails"); foreach (DataRow child in row.GetChildRows("p2c")) { Console.WriteLine("\tmyChild name:{0}\tOrder total:{1}", child["name"],child["orderTotal"]); } } } } }Cetin