【发布时间】:2020-04-05 10:42:29
【问题描述】:
我有一个包含许多表和值函数的数据库,是否有动态调用这些函数的方法。我使用.net core 3 web api。 请帮忙
【问题讨论】:
标签: c# sql asp.net-core-webapi
我有一个包含许多表和值函数的数据库,是否有动态调用这些函数的方法。我使用.net core 3 web api。 请帮忙
【问题讨论】:
标签: c# sql asp.net-core-webapi
我将使用 Dapper 执行此操作,然后使用 SP 或 select 语句以您想要的方式调用函数
以下是使用 Dapper & Select 语句从 API 获取数据的示例
//for EF
private readonly ApplicationDbContext _db;
//For Dapper
private readonly SqlConnectionConfiguration _configuration;
public NewsController(ApplicationDbContext db, SqlConnectionConfiguration configuration)
{
_db = db;
_configuration = configuration;
}
public async Task<IActionResult> GetAll()
{
//fetch data using EF
// return Json(new { data = await _db.News.OrderByDescending(x => x.NewsDate).ToListAsync() });
//Fetch data using Dapper
IEnumerable<News> newslist;
using (var conn = new SqlConnection(_configuration.Value))
{
string query = "select * FROM News";
conn.Open();
try
{
newslist = await conn.QueryAsync<News>(query, commandType: CommandType.Text);
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
return Json(new { data = newslist });
}
剩下的,您可以对下面的代码进行更改以使其适合您。
【讨论】: