c# - 如何获取数据库表中列名的 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
解决方案
有两种方法可以完成此任务:
- 第一个是使用 . NET 数据提供程序或通过将此查询添加到您的 EF 模型。
另一种是使用反射来访问 Account 类型属性:
typeof(Account).GetProperties(BindingFlags.Public|BindingFlags.Instance)
推荐阅读
- azure - 显示资源未找到消息的 Azure 警报报告
- javascript - 在 Reactjs 中如何将 api 数据插入到 ant 设计表中
- php - PHP MySQL函数计算输出
- c++ - 使用显式转换访问其他兄弟类成员函数的兄弟类如何在 C++ 中工作?
- python-3.x - 根据现有列中的数据创建新列
- python - 使用 Web Scraping 方法(Python BS4)从 Google Play 商店中提取 Android 应用程序的评论 - 索引超出范围
- vbscript - 从替换中跳过一个字符串,如果它有任何后缀或后缀
- android - 在服务内部使用广播接收器
- angular - Angular 8:NG8002:不能绑定到“ontime”,因为它不是“div”的已知属性
- node.js - 在win10中,我执行yarn add protobufjs,但是报错:error An unexpected error occurred: "EPERM: operation not allowed, unlink