-2

I have a DataSet ds which contains 10 DataTables, each have one column only. How to make a DataTable that contains all the columns from DataSet ds?


public static DataSet ReadXmlUsingBufferedStream(string pathOfXMLFile) 
{ 
   DataSet ds = new DataSet(); 
   ds.EnforceConstraints = false; 
   if (File.Exists(pathOfXMLFile)) 
   { 
      using (FileStream filestream = File.OpenRead(pathOfXMLFile)) 
      { 
         BufferedStream buffered = new BufferedStream(filestream);
         ds.ReadXml(buffered); 
      } 
   } 
   ds.EnforceConstraints = true; 
   return ds; 
}
Random Dev
  • 51,810
  • 9
  • 92
  • 119
Pranay Deep
  • 1,371
  • 4
  • 24
  • 44
  • How are you filling `DataSet`? show us that code. – Bharadwaj Feb 08 '16 at 06:36
  • 1
    hi @pranay is you mean you want new data table which will have 10 columns each from your 10 seprate data tables ? and does this all data tables have same numbers of rows? – Pushkar Phule Feb 08 '16 at 06:38
  • @PushkarPhule hi, yes. – Pranay Deep Feb 08 '16 at 06:39
  • @Bharadwaj public static DataSet ReadXmlUsingBufferedStream(string pathOfXMLFile) { DataSet ds = new DataSet(); ds.EnforceConstraints = false; if (File.Exists(pathOfXMLFile)) { using (FileStream filestream = File.OpenRead(pathOfXMLFile)) { BufferedStream buffered = new BufferedStream(filestream); ds.ReadXml(buffered); } } ds.EnforceConstraints = true; return ds; } – Pranay Deep Feb 08 '16 at 06:39
  • does this all data tables have same numbers of rows? – Pushkar Phule Feb 08 '16 at 06:40
  • http://stackoverflow.com/questions/12278978/combining-n-datatables-into-a-single-datatable is this solving your problem ? @pranay – Pushkar Phule Feb 08 '16 at 06:41
  • @PushkarPhule - yes it has same no. of rows – Pranay Deep Feb 08 '16 at 06:41
  • @PranayDeep please don't put additional code into comments - edit your question instead (the comment code is unreadable) – Random Dev Feb 08 '16 at 06:43
  • @PushkarPhule - Thanks for the link, but it is DataTable Class which i am talking about in c# .net – Pranay Deep Feb 08 '16 at 06:43
  • @pranay use this dtAll = dtOne.Copy(); dtAll.Merge(dtTwo); – Pushkar Phule Feb 08 '16 at 06:45
  • 1
    your problem is that you have no key to associate the values if you only have one column - so if you want to do it by index it should be as easy as two for-loops – Random Dev Feb 08 '16 at 06:46
  • 1
    Check your `xml` structure, it may be because of the structure it is creating new tables. Try using `DataTable.ReadXml()` to read it to the `DataTable` instead of `DataSet`. – Bharadwaj Feb 08 '16 at 06:50
  • Do you just need the scema of each table or just name or everithing related to it? – Abdur Rahim Feb 08 '16 at 06:51

1 Answers1

2

Here I just have marged all datatabes available in dataset. If you set any condition that, you will add certain columns of certain table, than this should be enhanced.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Building a dataset having 10 different datatable which contains 1 column each
            DataSet ds = new DataSet();            
            for (int i = 0; i <= 9; i++)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("dt" + i + "_column1", typeof(string));
                dt.AcceptChanges();
                ds.Tables.Add(dt);

            }
            ds.AcceptChanges();

            //Here finally building a datatable which consists all columns of each and every tables in dataset
            DataTable dtFinal = new DataTable();
            foreach (DataTable tmp in ds.Tables)
            {
                dtFinal.Merge(tmp);
            }
        }
    }
}
Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83