如何使用TableName获取特定的数据表

本文关键字:数据表 获取 何使用 TableName | 更新日期: 2023-09-27 18:11:39

一个数据集通常有很多数据表,但我只针对一个特定的数据表,我认为这是很正常的,但显然没有做到这一点?
以下是我尝试过的方法:

//Could not find an implementation of the query pattern for source type.......
DataTable dt = from table in changesDataSet.Tables
               where table.TableName = "ABC"
               select table;
//Surprisingly there was no method "Where" in changesDataSet.Tables
DataTable dt = changesDataSet.Tables.Where(x=>x.TableName="ABC").First();

下面是能够打印每个表的代码。我知道我可以通过循环来实现但是请告诉我循环不是唯一的选项

foreach(DataTable table in changesDataSet.Tables)
{
    Console.WriteLine(table.TableName);
}

如何使用TableName获取特定的数据表

您可以使用表集合(DataTableCollection)上的索引器访问表:

DataTable dt = changesDataSet.Tables["ABC"];