1

I have multiple data tables(upto 28) in C# and I want to append all their columns in one data table(with data). Example: datatables are: DT1,DT2,DT3 and so on Number of Columns in these respective data tables: N1,N2,N3 and so on Output: One data table DT with total columns N1+N2+N3+... and all the data from the above data tables. Thanks. Edit: My tables have clustered primary keys.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
DIVIJ
  • 83
  • 1
  • 8
  • so what type of data in your column n1,n2, n3? its int, string, double or any else? – er-sho Aug 28 '18 at 06:05
  • See this link https://stackoverflow.com/questions/12278978/combining-n-datatables-into-a-single-datatable – er-sho Aug 28 '18 at 06:07
  • @ershoaib the data can be of any type, each table have around 100 columns so in short I want to get a table consisting of around 2000 columns that includes all the DT1,DT2 etc. The number of rows will be same in the all tables. – DIVIJ Aug 28 '18 at 06:17
  • if your have 3 datatables with each of 5 columns so your want 15 coulmns in resulting datatable or only 5 column with concatenating of each of respective column in all 3 datatable – er-sho Aug 28 '18 at 06:20
  • @ershoaib the solution in the link cannot be implemented in my case as I have clustered primary key – DIVIJ Aug 28 '18 at 06:34
  • @NiranjanKala in that case the primary key is one column, I have a clustered primary key – DIVIJ Aug 28 '18 at 06:36
  • @ershoaib I need 15 columns. for example i have 3 datatables with 5 columns each and 10 rows in all three datatables, I need a datatable with 15 columns and 10 rows – DIVIJ Aug 28 '18 at 06:37
  • @DIVIJ: you can take idea from that code and modify according to your requirement. It will make you move forward to complete your task. – Niranjan Singh Aug 28 '18 at 06:52
  • clustered primary key and primary key are same read this https://stackoverflow.com/questions/10706992/database-primary-key-clustered-or-nonclustered but if your clustered index is rather than primary key then you please mention in your question. – er-sho Aug 28 '18 at 07:28

1 Answers1

1
var all = new DataTable();

all.Merge(DT1);
all.Merge(DT2);
all.Merge(DT2);

... better to use in a loop.

Roman Svitukha
  • 1,302
  • 1
  • 12
  • 22