如何从数据表中选择特定列

编程入门 行业动态 更新时间:2024-10-27 18:28:22
本文介绍了如何从数据表中选择特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在数据表中有列数,其中我想选择一个特定的库。 我尝试过: 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 [ ^ ]。

更多推荐

如何从数据表中选择特定列

本文发布于:2023-10-30 11:21:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542711.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!