如何从数据表中选择特定列 [英] How to select a particular column from datatable
本文介绍了如何从数据表中选择特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在数据表中有列数,其中我想选择一个特定的库。
我尝试过:
i尝试了多种方式,通过搜索谷歌
in a datatable there are number of columns ,out of which i want to select a particular coulmn.
What I have tried:
i have tried number of ways ,by searching on google
推荐答案
您可以使用Columns集合:
You can use the Columns collection:
using (SqlConnection con = new SqlConnection(strConnect))
{
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter("SELECT TOP 20 Title FROM Videos ORDER BY Title ASC", con))
{
da.Fill(dt);
}
DataColumn dc = dt.Columns[0];
}
但这不会让您访问表格每列中的实际值。
为此,你需要迭代Rows集合:
But that won't give you access to the actual values in each column of the table.
For that, you need to iterate the Rows collection:
foreach (DataRow row in dt.Rows)
{
Console.WriteLine(row[0]);
}
您可以从DataTable的行访问该列。就像任何数据表一样,您首先访问行,然后访问它们的列。
You can access the column from the rows of the DataTable. Just like any data table, you first access the rows and then you access their columns.
var table = GetYourDataTable();
foreach (var row in table) {
var column = row["ColumnName"];
// Use the column
}
该表将提供对行的访问,然后您可以使用它们的名称或索引来单独访问每个列。这个MSDN指南不是关于获取列,而是让您了解如何获取列(用于getter和setter操作)。 将数据添加到DataTable [ ^ ]。
这篇关于如何从数据表中选择特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文