10
votes

I have denormalized data in a DataTable.

The data contains employee names, and the pay they got over a series of pay cycles. i.e.:

My DataTable contains:

Employee 1    Jan-1-2012     $100
Employee 2    Jan-1-2012     $300
Employee 1    Feb-1-2012     $400
Employee 2    Feb-1-2012     $200
Employee 1    Mar-1-2012     $150
Employee 2    Mar-1-2012     $325

How can load this data into a DataSet where the parent DataTable contains the employees name, and the child DataTable contains details of the paycheck?

3
Your question is not specific enough, that's why you're getting the answers below. If you provide examples of how you want the "parent" and "child" datatables to look like, then maybe other people can give the answer you're expecting.Steven Manuel

3 Answers

18
votes

DataSet is nothing but a collection of DataTables. So to "load" the dataTable into dataSet simple Add it:

        DataTable employees = new DataTable();
        DataTable payCheckes = new DataTable();
        DataSet ds = new DataSet();
        ds.Tables.Add(employees);
        ds.Tables.Add(payCheckes);

Do you want to "combine" datatables somehow? Get paycheckes of each employee?

4
votes

the code without manual insert:

       DataSet ds = new DataSet();
        DataTable dtemploye = new DataTable();
        DataTable dtpayment = new DataTable();

        ds.Tables.AddRange(new DataTable[] { dtemploye, dtpayment });
        DataColumn dcIdemploye = dtemploye.Columns["ID_EMPLOYEE"];
        DataColumn dcIdemployeprice = dtpayment.Columns["ID_EMPLOYEE"];
        DataRelation drrelation = new DataRelation("relemploy_payment", dcIdemploye, dcIdemployeprice);
        ds.Relations.Add(drrelation);
1
votes
      DataSet ds = new DataSet();
        DataTable dtemploye = new DataTable();
        DataColumn dcnameemploye = new DataColumn();
        DataColumn dcIdemploye = new DataColumn();
        dtemploye.Columns.AddRange(new DataColumn[]{dcnameemploye,dcIdemploye});

        DataTable dtpayment = new DataTable();
        DataColumn dtprice = new DataColumn();
        DataColumn dtDate = new DataColumn();
        DataColumn dcIdemployeprice = new DataColumn();
        dtpayment.Columns.AddRange(new DataColumn[]{dcIdemployeprice,dtprice,dtDate});

        DataRow drrowemploy = dtemploye.NewRow();
        drrowemploy[0] = "1";
        drrowemploy[1] = "Employee 1";
        dtemploye.Rows.Add(drrowemploy);

        DataRow drrowpayment = dtpayment.NewRow();
        drrowpayment[0] = "1";
        drrowpayment[0] = "01/01/2012";
        drrowpayment[1] = " 300";


        ds.Tables.AddRange(new DataTable[]{dtemploye, dtpayment});

        DataRelation drrelation = new DataRelation("relemploy_payment", dcIdemploye, dcIdemployeprice);
        ds.Relations.Add(drrelation);