Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i have 4 datatable. I need to make it one single table and I need to keep all duplicate columns also.Merge methode missing some data.I need to join all tables and to a single one and should keep duplicate columns also.I used below codes but i am missing some coloumn (duplicates also)


What I have tried:

DataTable dtFinal = new DataTable();

            for (int i = 0; i < resultDS.Tables.Count; i++)

            {

                if (i == 0)

                    dtFinal = resultDS.Tables[i].Copy();

                else

                    dtFinal.Merge(resultDs.Tables[i]);

            }



            var ds = new DataSet();

            ds.Tables.Add(dtFinal);
Posted
Updated 2-Feb-22 5:49am
Comments
CHill60 2-Feb-22 10:00am    
It might help your explanation if you included some sample data and then the results you expect from that sample data.
If by "keep duplicate columns" you mean that you have a column in each table that has the same name but contains different data then you can't just merge - you cannot have two columns of the same name in the final datatable. It's usual to "adjust" the schema of the 2nd table and then merge it with the first
[no name] 2-Feb-22 10:20am    
same column name but diffrent data i have

1 solution

I have created a demo for multiple tables which returning duplicate data as well and after that merge into single table. See the code below:

SqlDataAdapter adapter = new SqlDataAdapter("procGetEmployee", sqlConn);
DataSet resultDS = new DataSet();
adapter.Fill(resultDS, "dsEmployees");

DataTable dtFinal = new DataTable("dtEmployee");
for (int i = 0; i < resultDS.Tables.Count; i++)
{
    dtFinal.Merge(resultDS.Tables[i]);
}

Result: All tables merged into single table that is dtFinal
 
Share this answer
 
v2
Comments
[no name] 2-Feb-22 13:00pm    
not working as expected.. here am getting only 3 columns.I dont need to remove duplicates.below am trying


var Dset = new DataSet();

DataTable dt1 = new DataTable();

dt1.Columns.Add("Name", typeof(string));

dt1.Columns.Add("Age", typeof(int));

// Create a DataRow, add Name and Age data, and add to the DataTable

DataRow dr1 = dt1.NewRow();

dr1["Name"] = "Mohammadsd"; // or dr[0]="Mohammad";

dr1["Age"] = 24; // or dr[1]=24;

dt1.Rows.Add(dr1);

Dset.Tables.Add(dt1);



DataTable dt2 = new DataTable();

dt2.Columns.Add("Name", typeof(string));

dt2.Columns.Add("Age", typeof(int));

// Create a DataRow, add Name and Age data, and add to the DataTable

DataRow dr2 = dt2.NewRow();

dr2["Name"] = "Mohammad"; // or dr[0]="Mohammad";

dr2["Age"] = 24; // or dr[1]=24;

dt2.Rows.Add(dr2);

Dset.Tables.Add(dt2);



DataTable dt3 = new DataTable();

dt3.Columns.Add("Name", typeof(string));

dt3.Columns.Add("sex", typeof(int));

// Create a DataRow, add Name and Age data, and add to the DataTable

DataRow dr3 = dt3.NewRow();

dr3["Name"] = "last"; // or dr[0]="Mohammad";

dr3["sex"] = 24; // or dr[1]=24;

dt3.Rows.Add(dr3);

Dset.Tables.Add(dt3);







DataTable dtFinal = new DataTable("dtEmployee");

for (int i = 0; i < Dset.Tables.Count; i++)

{

dtFinal.Merge(Dset.Tables[i]);

}

var ds = new DataSet();

ds.Tables.Add(dtFinal);
M Imran Ansari 2-Feb-22 16:00pm    
I have modified the code, please check now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900