【问题标题】:Getting Column Names of a Table Ado.NET获取表的列名 Ado.NET
【发布时间】:2014-04-14 19:46:03
【问题描述】:

我正在尝试获取表的列名,但它只返回列名,例如TABLE_SCHEMATABLE_NAMETABLE_CATALOGTABLE_TYPE。 这是代码;

DataTable table = con.GetSchema("Tables", new string[] { null, null, "Contact" });
var columnNames = (from DataRow r in table.Rows
                               from DataColumn c in table.Columns
                               select c.ColumnName).ToList();

我需要将姓名保存在一个列表中。谢谢。

【问题讨论】:

    标签: datatable ado.net


    【解决方案1】:

    试试这个:

    DataTable table = con.GetSchema("Columns", new string[] { null, null, "Contact" });
    

    您必须指定 Columns 而不是 Tables 作为 collectionName ...

    您还应该将 linq 查询更改为以下内容:

    var columnNames = (from r in tab.AsEnumerable() 
                            select r.Field<string>("COLUMN_NAME")).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-02
      • 2015-10-04
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 2011-04-16
      相关资源
      最近更新 更多