如何从表或结果集中获取列名和其他元数据 [英] How to get column names and other metadata from a table or resultset
问题描述
EX在数据表中
- 从水分到末端的列类型为布尔值
- 我要获取组合框中数据为 1的列名
具体来说,对于结果集的每一行,我都需要那些包含值 1
的列名称的集合,以便我可以填充组合框。
Specifically, for each row of my result set, I need a collection of those column names which contain the value 1
so I can populate a combobox.
对不起,我的英语我不够好。
Sorry for my English I'm not good enough.
推荐答案
在读取数据库时使用MySQL .net连接器(和任何RDMS连接器)查询的结果集,您将拥有一个DataReader对象。在MySQL的情况下,它是MySqlDataReader。
Using the MySQL .net connector (and any RDMS connector) when you are reading a result set from a query, you will have a DataReader object. In MySQL's case it is a MySqlDataReader.
例如(来自 http://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-sql-command.html )
string sql = "SELECT * FROM data";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
/* iterate once per row */
Console.WriteLine(rdr[0]+" -- "+rdr[1]); /* or whatever */
}
rdr.Close();
拥有有效的DataReader后,可以使用 GetSchemaTable()
方法来获取描述结果集的数据的 DataTable
集合。此信息来自MySQL,是结果集的一部分。例如:
Once you have a valid DataReader, you can use the GetSchemaTable()
method to obtain a DataTable
collection of data describing the result set. This information comes from MySQL as part of the result set. For example:
MySqlDataReader rdr = cmd.ExecuteReader();
DataTable schema = rdr.GetSchemaTable();
此 DataTable
每列包含一行在结果集中。您可以像这样访问有关结果集中各列的有用信息:
This DataTable
contains a row for each column in the result set. You can access useful information about your columns in your result set like this:
foreach (DataRow rdrColumn in schema.Rows) {
String columnName = rdrColumn[schema.Columns["ColumnName"]].ToString();
String dataType = rdrColumn[schema.Columns["DataType"]].ToString();
}
还有名为 ColumnSize $ c的项目$ c>,
NumericPrecision
, NumericScale
,依此类推。如果需要,这些可用于结果集中的每一列。
There are also items named ColumnSize
, NumericPrecision
, NumericScale
, and so forth. Each of these is available for each column in the result set if you need them.
编辑
您可以制作一个包含结果集列名称的字典,如下所示:
Edit You can make a Dictionary holding the names of the result set's columns like this:
Dictionary<int,String> columnNames = new Dictionary<int,string>();
int index = 0;
foreach (DataRow row in schema.Rows) {
columnNames.Add(index,row[schema.Columns["ColumnName"]].ToString());
index++;
}
此后,当您遍历行时,可以创建列列表
Thereafter, as you iterate through the rows, you can create a List of columns, by name, with a certain row value.
while (rdr.Read()) {
/* for each row */
List<String> listOfColumns = new List<string>();
for (int i = 0; i < rdr.FieldCount; i++) {
var val = rdr[i];
if ("1" == val) {
/* if the value of the column is 1, add the column name from the dictionary */
listOfColumns.Add(columnNames[i]);
}
}
}
例如看结果的例子设置元数据请参见此处。 http://etutorials.org/Programming/ado+net/Part+I+ADO.NET+Tutorial/第5章。+ DataReaders / 5.4 + DataReaders + and + Schema + Information /
For examples of looking at result set metadata see here. http://etutorials.org/Programming/ado+net/Part+I+ADO.NET+Tutorial/Chapter+5.+DataReaders/5.4+DataReaders+and+Schema+Information/
这篇关于如何从表或结果集中获取列名和其他元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!