如何从DataSet获取DataTable?

时间:2016-02-08 06:34:34

标签: c# .net linq datatable

我有一个DataSet ds,其中包含10个DataTable,每个只有一列。如何制作包含DataTable的所有列的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; 
}

1 个答案:

答案 0 :(得分:2)

我只是在datatabes中提供了所有dataset。如果你设置了任何条件,你将添加某些表的某些列,而不应该增强它。

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