How can I convert a datatable to a related dataset
Asked Answered
G

4

11

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?

Giulietta answered 29/7, 2012 at 16:13 Comment(1)
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.Peat
B
21

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?

Bouilli answered 29/7, 2012 at 16:20 Comment(3)
I'm not sure how what you are saying has any relevance to what I am asking.Giulietta
@Giulietta - maybe we're not sure what you're asking.Paresis
Exactly as @Henk stated, you didnt ask any such exact question, like how to create a relation between datatables, or something similar. Please go ahead and ask it.Bouilli
F
4

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);
Fides answered 29/7, 2012 at 16:52 Comment(1)
for dcIdemploye you have to bind this column to the correct parent column that is dcIdemploye = dtemploye.Columns["ID_EMPLOYEE"]Fides
F
1
      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);
Fides answered 29/7, 2012 at 16:30 Comment(4)
I'm not sure how what you are saying has any relevance to what I am asking.Giulietta
hello, try it.it load data on 2 datatable and establish a relation between datatable parent (employee) and datatable child (payment)Fides
for sure if your data come from sql server you don't have to insert data manuallyFides
you will use a dataAdapter to fill datatableFides
N
0
            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);
Nowhither answered 15/7, 2023 at 10:15 Comment(1)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Hildy

© 2022 - 2024 — McMap. All rights reserved.