首页 > 解决方案 > 如何获取数据库表中列名的 JSON 响应

问题描述

我想在 API 端的控制器类中编写一个方法来返回所有列名的列表。该方法会是什么样子,它会返回什么?我将如何在客户端检索这个“列表”?

private CoreGradingDBEntities db = new CoreGradingDBEntities();

// GET: api/
public IQueryable<Account> GetAccounts()
{
  return db.Accounts;
}


//Get field names
//What is the response type
public IHttpActionResult GetFieldNames()
{
  //Query goes here, gets executed and returns all column names?
}

// GET: api/Accounts/5
[ResponseType(typeof(Account))]
public IHttpActionResult GetAccount(string id)
{
    Account account = db.Accounts.Find(id);
    if (account == null)
    {
         return NotFound();
    }
    return Ok(account);
}

一个返回列名的查询,我想要执行并作为 HTTP 响应获取。

SELECT COLUMN_NAME

FROM INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_NAME = 'Account'

ORDER BY ORDINAL_POSITION

标签: c#entity-frameworkapi

解决方案


有两种方法可以完成此任务:


推荐阅读