How to Merge a Dataset, datarow, Datatable in a Dataset Using C#


Today i want to demonstrate How Merge function will work in Asp.net using C# ? If you want to Merge Datarows or Dataset or Datatables in a Dataset How you achieve this using .Net in c# ? I will write Code snippets and with real time images...

Some times in your Project we need to Combine the Data that means two result sets in to One result set. How it is Possible in Asp.net using C# this is the Article i want to discuss here .


The Merge() Function will have 7 overloaded arguments these are the Following....



DATASET dst =new DataSet();

1) dst.Merge([]datarows)

2) dst.Merge(Dataset)

3) dst.Merge(DataTable)

4.) dst.Merge(dataset,bool preservechanges) preserving and discarding according to the Argument

5)dst.Merge([]dr,bool preservechanges,MissingSchemaAction) handling incompatiable to the Schema given arguments

6)dst.Merge(dataset,,bool preservechanges,MissingSchemaAction) handling incompatiable to the Schema given arguments

7)dst.Merge(datatble,,bool preservechanges,MissingSchemaAction) handling incompatiable to the Schema given arguments



Now Look at the Code below how it works

In this First Example Look how datarows are Merged in a New Dataset



DataSet dst = new DataSet();
DataTable dt =new DataTable();
dst.Tables.Add(dt);

DataColumn dc = new DataColumn();
dc.ColumnName="Name";
dst.Tables[0].Columns.Add(dc);
DataColumn dc1 = new DataColumn();
dc1.ColumnName = "Class";
dst.Tables[0].Columns.Add(dc1);
DataColumn dc2 = new DataColumn();
dc2.ColumnName = "Phone";
dst.Tables[0].Columns.Add(dc2);

DataRow dr = dst.Tables[0].NewRow();
dr[0] = "Bhushan";
dr[1] = "Inter";
dr[2] = "04024546134";

DataRow dr1 = dst.Tables[0].NewRow();
dr1[0] = "kambampati";
dr1[1] = "B.sc";
dr1[2] = "0402412048";

DataRow dr2 = dst.Tables[0].NewRow();
dr2[0] = "Phani";
dr2[1] = "M.sc";
dr2[2] = "0402412048";


dst.Tables[0].Rows.Add(dr);
dst.Tables[0].Rows.Add(dr1);
dst.Tables[0].Rows.Add(dr2);


// To get Datarows....

DataRow []drs = dst.Tables[0].Select("Name='Bhushan'");

DataSet dst2 = new DataSet();
dst2.Merge(drs);




In runtime you see how the Output looks like

see the below dataset Visualizer the data which in array of datarow is Merged in the new Dataset...

DATAROWMERGE

See how a Dataset Merges to a New Dataset ....

Datasetmerges

Dataset Merges with a Datatable...


DataSet dst = new DataSet();
DataTable dt =new DataTable();
dst.Tables.Add(dt);

DataColumn dc = new DataColumn();
dc.ColumnName="Name";
dst.Tables[0].Columns.Add(dc);

DataColumn dc1 = new DataColumn();
dc1.ColumnName = "Class";
dst.Tables[0].Columns.Add(dc1);

DataColumn dc2 = new DataColumn();
dc2.ColumnName = "Phone";
dst.Tables[0].Columns.Add(dc2);

DataRow dr = dst.Tables[0].NewRow();
dr[0] = "Bhushan";
dr[1] = "Inter";
dr[2] = "04024546134";

DataRow dr1 = dst.Tables[0].NewRow();
dr1[0] = "kambampati";
dr1[1] = "B.sc";
dr1[2] = "0402412048";

DataRow dr2 = dst.Tables[0].NewRow();
dr2[0] = "Phani";
dr2[1] = "M.sc";
dr2[2] = "0402412048";


dst.Tables[0].Rows.Add(dr);
dst.Tables[0].Rows.Add(dr1);
dst.Tables[0].Rows.Add(dr2);

// Datatable ...

DataTable dtab = new DataTable();


DataColumn dcab = new DataColumn();
dcab.ColumnName = "Name";
dtab.Columns.Add(dcab);

DataColumn dcab1 = new DataColumn();
dcab1.ColumnName = "Class";
dtab.Columns.Add(dcab1);


DataColumn dcab2 = new DataColumn();
dcab2.ColumnName = "Phone";
dtab.Columns.Add(dcab2);

DataRow drab = dtab.NewRow();
drab[0] = "Kp";
drab[1] = "Inter";
drab[2] = "6778990000000";
dtab.Rows.Add(drab);

DataRow drrab1 = dtab.NewRow();
drrab1[0] = "BK";
drrab1[1] = "B.sc";
drrab1[2] = "89067546466";
dtab.Rows.Add(drrab1);

DataRow drrab2 = dtab.NewRow();
drrab2[0] = "Phani";
drrab2[1] = "SSC";
drrab2[2] = "89067546466";
dtab.Rows.Add(drrab2);

dst.Tables[0].Merge(dtab);



Look at the Dataset Visualizer here....

datatabmergers


Attachments

Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 73 articles authored by srirama

Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: