简体   繁体   English

如何从数据集中检索特定列?

[英]How to retrieve specific column from the dataset?

Here's the lines that are querying the database. 这是查询数据库的行。

string sql = "SELECT col1, col2, col3, col4, col5"
           + "FROM MyTable"
           + "WHERE condition";

DataSet standardResults = SqlHelper.ExecuteDataset(
    m_ConnectionString,
    CommandType.Text,
    sql);

For each column, I'd like to select col1 and col5 . 对于每一列,我想选择col1col5

I'm trying to hover over the dataset object or drill down the content, but I can't figure out where the data I need are located. 我正在尝试将鼠标悬停在数据集对象上或向下钻取内容,但我无法确定我需要的数据位于何处。

Thanks for helping. 谢谢你的帮助。

From the code you have posted, I can assume that the SqlHelper.ExecuteDataset returns a DataSet . 从您发布的代码中,我可以假设 SqlHelper.ExecuteDataset返回一个DataSet A DataSet contains a collection of DataTables and you can use a index to get them. DataSet包含DataTables的集合,您可以使用索引来获取它们。 In your case there would be only one DataTable , since you have the results of one query. 在您的情况下,只有一个DataTable ,因为您有一个查询的结果。 So standardResults.DataTables[0] would be associated with the DataTable where your data are in. That being said, I think that you can fairly query your DataTable with LINQ and get that you want. 因此, standardResults.DataTables[0]将与您的数据所在的DataTable相关联。也就是说,我认为您可以使用LINQ公平地查询您的DataTable并获得您想要的。 Something like this: 像这样的东西:

var results = standardResults.DataTables[0]
                             .AsEnumerable()
                             .Select(item=>new 
                             {
                                Col1 = x.Field<string>("col1"),
                                Col5 = x.Field<string>("col5")    
                             });

However, I have to point out that I don't see the reason of selecting the extra columns, col2 , col3 and col4 . 但是,我必须指出,我没有看到选择额外列, col2col3col4 If you don't need them, why they are in the select statement? 如果您不需要它们,为什么它们在select语句中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM