如何在 ASP.NET Core 3.1 中执行存储过程并将结果集分配给变量

问题描述 投票:0回答:1

我是 ASP.NET Core (3.1) 的新手。我有一个带有两个参数的存储过程:

startDate
endDate

它返回以下结果集:

我希望能够获取 3 个单独变量中的结果集,因为我希望将它们输出到我的视图中:

例如 - 对于第一个结果集:

foreach (var maindata in Model.MainDataViewModel)
{
     <td>maindata.CaseId</td>
     <td>maindata.EpisodeId</td>
     <td>maindata.SpecimenTag</td>
     <td>maindata.PathologyOrderId</td>
}

第二个和第三个也是如此。

我尝试使用此代码调用存储过程:

public SpPotentialCandidatesForOncotypeDXViewModel GetPotentialCandidatesForTheOncotypeDXData(DateTime startDate, DateTime endDate)
{
    try
    {
        List<SqlParameter> pc = new List<SqlParameter>
                        {
                           new SqlParameter("@p0", startDate),
                           new SqlParameter("@p1", endDate),
                        };
        
        var da = _dbContext.Database.ExecuteSqlRaw("spPotentialCandidatesForOncotypeDX @p0, @p1", pc.ToArray());
    }
    catch (Exception ex)
    {
        AppLog.WriteError("GetPotentialCandidatesForTheOncotypeDXData", ex.Message);
    }

    // return spPotentialCandidatesForOncotypeDXes;
}

经过测试,

da
的值为-1。通过进一步的研究,我意识到
ExecuteSqlRaw
返回受影响的行数,而不是结果集。

如何单独捕获这些结果集并分配给变量?

c# asp.net-core stored-procedures entity-framework-core dapper
1个回答
0
投票

ExecuteSqlRaw 用于执行不返回实体但影响行的 SQL 命令(例如 UPDATE、INSERT 或 DELETE)。如果您想捕获结果集,通常会使用类似 FromSqlRaw 的方法。

根据您的需求,您可以这样做:

定义 ViewModel 类来映射存储过程的结果:

public class MainDataViewModel
{
    public int CaseId { get; set; }
    public int EpisodeId { get; set; }
    public string SpecimenTag { get; set; }
    public int PathologyOrderId { get; set; }
}

public class SecondDataViewModel
{
    // Define properties similar to MainDataViewModel based on your second result set
}

public class ThirdDataViewModel
{
    // Define properties similar to MainDataViewModel based on your third result set
}

public class SpPotentialCandidatesForOncotypeDXViewModel
{
    public List<MainDataViewModel> MainDataViewModels { get; set; }
    public List<SecondDataViewModel> SecondDataViewModels { get; set; }
    public List<ThirdDataViewModel> ThirdDataViewModels { get; set; }
}

执行存储过程并将结果映射到您的 ViewModel 类:

public SpPotentialCandidatesForOncotypeDXViewModel GetPotentialCandidatesForTheOncotypeDXData(DateTime startDate, DateTime endDate)
{
    var model = new SpPotentialCandidatesForOncotypeDXViewModel();

    try
    {
        var pc = new List<SqlParameter>
        {
            new SqlParameter("@p0", startDate),
            new SqlParameter("@p1", endDate),
        };

        model.MainDataViewModels = _dbContext.Set<MainDataViewModel>()
            .FromSqlRaw("spPotentialCandidatesForOncotypeDX @p0, @p1", pc.ToArray()).ToList();

        // Assuming the stored procedure returns 3 separate result sets, you'll need to modify the stored procedure and execute it three times
        // for the 3 different data mappings.

        // model.SecondDataViewModels = _dbContext.Set<SecondDataViewModel>().FromSqlRaw("YourProcedureForSecondData @p0, @p1", pc.ToArray()).ToList();
        // model.ThirdDataViewModels = _dbContext.Set<ThirdDataViewModel>().FromSqlRaw("YourProcedureForThirdData @p0, @p1", pc.ToArray()).ToList();
    }
    catch (Exception ex)
    {
        AppLog.WriteError("GetPotentialCandidatesForTheOncotypeDXData", ex.Message);
    }

    return model;
}

然后在您的视图中,使用您在问题中演示的返回的模型。

注意:该示例假设存储过程将针对三种不同的数据映射执行三次。如果您的存储过程一次性返回 3 个结果集,那么此方法将不起作用,您必须研究更专门的方法或库来使用 Entity Framework Core 处理多个结果集。

© www.soinside.com 2019 - 2024. All rights reserved.