Pranay Deep February 2016

How to get DataTable From DataSet?

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; 
}

Answers


Abdur Rahim February 2016

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);
            }
        }
    }
}

Post Status

Asked in February 2016
Viewed 3,295 times
Voted 8
Answered 1 times

Search




Leave an answer