C# 使用 GetSchemaTable() 仅检索列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12160976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:18:27  来源:igfitidea点击:

Using GetSchemaTable() to retrieve only column names

c#sql

提问by user1495962

Is it possible to use GetSchemaTable()to retrieve only column names?

是否可以GetSchemaTable()仅用于检索列名?

I have been trying to retrieve Column names (only) using this method, is it possible.

我一直在尝试使用这种方法检索列名(仅),这可能吗。

  DataTable table = myReader.GetSchemaTable();

  foreach (DataRow myField in table.Rows)
  {
      foreach (DataColumn myProperty in table.Columns)
      {
          fileconnectiongrid.Rows.Add(myProperty.ColumnName + " = " 
                            + myField[myProperty].ToString());
      }
  }

This code retrieves a lot of table data unwanted, I only need a list containing column names!:

这段代码检索了很多不需要的表数据,我只需要一个包含列名的列表!:

采纳答案by Tim Schmelter

You need to use ExecuteReader(CommandBehavior.SchemaOnly)):

您需要使用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();
        }
    }
}

SchemaOnly:

仅架构

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.

查询仅返回列信息。使用 SchemaOnly 时,用于 SQL Server 的 .NET Framework 数据提供程序在使用 SET FMTONLY ON 执行的语句之前。

The column name is in the first column of every row. I don't think that it's possible to omit the other column informations like ColumnOrdinal,ColumnSize,NumericPrecisionand so on since you cannot use reader.GetStringbut only reader.GetSchemaTablein this case.

列名位于每行的第一列。我认为不可能省略其他列信息,例如ColumnOrdinal,ColumnSize,NumericPrecision等等,因为您不能使用reader.GetString但仅reader.GetSchemaTable在这种情况下使用。

But your loop is incorrect if you only want the column names:

但是如果你只想要列名,你的循环是不正确的:

foreach (DataRow col in schema.Rows)
{
    Console.WriteLine("ColumnName={0}", col.Field<String>("ColumnName"));
}

回答by code4life

Change your code to below if all you want is to display the column names. Your original code was trying to not only display column names, but also trying to display the actual data values as well.

如果您只想显示列名,请将您的代码更改为下面的代码。您的原始代码不仅试图显示列名,还试图显示实际数据值。

DataTable table = myReader.GetSchemaTable();

foreach (DataRow myField in table.Rows)
{
    foreach (DataColumn myProperty in table.Columns)
    {
        fileconnectiongrid.Rows.Add(myProperty.ToString());
    }
}

回答by Opster Elasticsearch - Nathan

This will give you all column names, you can place them in a string[]and do with them what you like.

这将为您提供所有列名称,您可以将它们放在 a 中,string[]并随心所欲地使用它们。

foreach(var columnName in DataTable.Columns)
{
  Console.WriteLine(columnName);
}

回答by Romita Dinda

//Retrieve column schema into a DataTable.
schemaTable = reader.GetSchemaTable();

int index = schemaTable.Columns.IndexOf("ColumnName");
DataColumn columnName = schemaTable.Columns[index];

//For each field in the table...
foreach (DataRow myField in schemaTable.Rows)
  {
    String columnNameValue = myField[columnName].ToString();
    Console.WriteLine("ColumnName " + columnNameValue);
  }