How to convert data to an appropriate data set

I have denormalized data in a DataTable.

The data contains the names of employees and the payment they received for the payment cycle. 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 to load this data into a DataSet where the parent DataTable contains the name of the employees and the child DataTable contains the salary information?

+6
source share
3 answers

A DataSet is nothing more than a collection of DataTables. Thus, to “load” dataTable into a dataSet, simply 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 somehow "merge" the data? Receive salaries for each employee?

+11
source

code without manual insertion:

  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); 
+3
source
  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); 
+1
source

Source: https://habr.com/ru/post/921612/


All Articles