您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

从没有数据的查询中获取列名

从没有数据的查询中获取列名

要做的第一件事是确保没有数据被返回:

SELECT TOP 0 [vwGetData].* FROM [vwGetData] WHERE 1 = 2;

现在假设您知道如何设置一个DataReader,您将执行以下操作:

using(var reader = command.ExecuteReader())
{
  // This will return false - we don't care, we just want to make sure the schema table is there.
  reader.Read();

  var tableSchema = reader.GetSchemaTable();

  // Each row in the table schema describes a column
  foreach (DaTarow row in tableSchema.Rows)
  {
    Console.WriteLine(row["ColumnName"]);
  }
}

您也可以查看SQL Catalog SYS视图

其他 2022/1/1 18:38:08 有536人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶