Monday, 17 December 2012

Create a relationship between two DataSet tables

Create a relationship between two DataSet tables

The DataRelation class is used to joins tables in DataSet. You can found DataRelation class within the System.Data namespace. Each relationship includes a parent and a child. Add two tables one for parent and another one for child to your DataSet to create a relationship between them. Then create a new DataRelation instance, passing its constructor the name of the new relationship, plus a reference to the linking columns in each table.

  DataSet objDS = new DataSet();
            objDS.Tables.Add(Employee);
            objDS.Tables.Add(Department);
            DataRelation objRelation = new DataRelation("objRelation",
            Employee.Columns["EmpID"], Department.Columns["EmpID"]);
            objDS.Relations.Add(objRelation);    

In above example we are joining an Employee table with a Department table, linking theEmployee.EmpID column as the parent with the related Department.EmpID column as the child

No comments:

Post a Comment