- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
是否可以使用 GetSchemaTable()
仅检索列名?
我一直在尝试使用这种方法(仅)检索列名,是否可能。
DataTable table = myReader.GetSchemaTable();
foreach (DataRow myField in table.Rows)
{
foreach (DataColumn myProperty in table.Columns)
{
fileconnectiongrid.Rows.Add(myProperty.ColumnName + " = "
+ myField[myProperty].ToString());
}
}
这段代码检索了很多不需要的表数据,我只需要一个列表包含列名!:
最佳答案
您需要使用 ExecuteReader(CommandBehavior.SchemaOnly))
:
DataTable schema = null;
using (var con = new SqlConnection(connection))
{
using (var schemaCommand = new SqlCommand("SELECT * FROM table", con))
{
con.Open();
using (var reader = schemaCommand.ExecuteReader(CommandBehavior.SchemaOnly))
{
schema = reader.GetSchemaTable();
}
}
}
The query returns column information only. When using SchemaOnly, the .NET Framework Data Provider for SQL Server precedes the statement being executed with SET FMTONLY ON.
列名位于每一行的第一列。我不认为可以省略其他列信息,例如 ColumnOrdinal、ColumnSize、NumericPrecision
等,因为您不能使用 reader.GetString
而只能使用 reader .GetSchemaTable
在这种情况下。
但是如果你只想要列名,你的循环是不正确的:
foreach (DataRow col in schema.Rows)
{
Console.WriteLine("ColumnName={0}", col.Field<String>("ColumnName"));
}
关于c# - 使用 GetSchemaTable() 仅检索列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12160976/
-你好,世界!-。我正在做一个带有ASP.NET的C#项目,我遇到了一个障碍。该项目是从表动态加载元数据和记录来编辑它们,而不是静态地定义哪些表可以编辑。因此,我需要获取不同表的模式/元数据。。以下是
OleDbDataReader oleDbDataReader = oleDbCommand.ExecuteReader(CommandBehavior.KeyInfo); DataTable dat
我是一个新手,我正在尝试从我的数据库中的某个表中检索列名称、大小(最大长度)和数据类型,当我执行以下代码时,它希望它显示所有列类型和名称(我没有找到如何引用 Size ,我使用了 ColumnSize
是否可以使用 GetSchemaTable() 仅检索列名? 我一直在尝试使用这种方法(仅)检索列名,是否可能。 DataTable table = myReader.GetSchemaTable
我正在尝试使用序数值从 MySqlDataReader 的每一行中提取数据,而不是使用字符串查找。为了做到这一点,我使用了阅读器提供的 GetSchemaTable 来获取要循环的数据表,以填充一个字
我正在编写一个类,它封装了使用 ADO.NET 从数据库中检索数据的复杂性。其核心方法是 private void Read(Action action) where T : class, new()
我是一名优秀的程序员,十分优秀!