MillinMo
MillinMo

Reputation: 435

How can I convert a datatable to a related dataset

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?

Upvotes: 11

Views: 81310

Answers (4)

Mitja Bonca
Mitja Bonca

Reputation: 4546

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 paychecks of each employee?

Upvotes: 21

Raj kumar.J.S
Raj kumar.J.S

Reputation: 1

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            dt.TableName = "Table1";
            dt.Columns.Add("Sno", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Address",typeof(string));
            DataRow Dr = dt.NewRow();

            Dr["Sno"] = 1;
            Dr["Name"] = "Test";
            Dr["Address"] = "Test1";

            dt.Rows.Add(Dr);

            ds.Tables.Add(dt);

Upvotes: 0

Hassan Boutougha
Hassan Boutougha

Reputation: 3919

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);

Upvotes: 4

Hassan Boutougha
Hassan Boutougha

Reputation: 3919

      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);

Upvotes: 1

Related Questions