c# - 动态检索存储过程的结果
问题描述
我有一个如下所示的休息端点。现在基于 ReportSource 名称,我想执行该存储过程。有 40 个这样的存储过程,我不想为每个存储过程创建实体类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BAService.Dtos
{
class BARequestDTO
{
public int ReportID { get; set; }
public string ReportName { get; set; }
public DateTime? AsOfDate { get; set; }
public string ReportSource { get; set; }
}
}
这是控制器:
[HttpPost]
[Route("BAReportRequest")]
public string BAReportRequest(BARequestDTO dto)
{
try
{
var test = _bIReportRepositoryBA.RunReport(dto);
}
catch (Exception ex)
{
_LogService.Error(string.Format("Unable to save data dictionary"), ex);
}
return test;
}
我知道执行存储过程的唯一方法是像下面这样调用它。在这种方法中,我将不得不创建 40 个可以从数据库接收结果的新实体对象。我很懒,我正在寻找一种执行存储过程并在 json 中获取结果而不为每个存储过程创建实体对象的通用方法。
public List<EmployerDashboardDetailsDto> RunReport(BIReportDto dto)
{
return _DBContext.Database
.SqlQuery<usp_EmployerDashboardStatistics>("report.usp_Report1 @exchangeID", new SqlParameter("@exchangeID", dto.ID))
.ToList();
}
解决方案
推荐阅读
- c - Create function which sorts 2 given sorted strings
- node.js - 服务器没有启动
- python - what's wrong with my python code in Choosing different function based on user input?
- matrix - 具有 Julia 的 ODE 的质量矩阵(雅可比稀疏性)
- python - Compare adjacent characters in string for differing case
- javascript - javascript, jQuery and ajax function not working properly in asp.net MVC
- javascript - How do I return location.hostname without the trailing slash?
- reactjs - 过滤音频剪辑列表时,React 应用程序中的音频标签播放错误的音频剪辑
- python - 用 np.polyfit 推断曲线会产生意想不到的结果 Python Pandas
- c - 索引比指针更容易矢量化吗?