简体   繁体   English

如何从存储过程 EF Core 3.1 中获取多个 OUTPUT 参数

[英]How to get Multiple OUTPUT parameters from stored procedure EF Core 3.1

I have been trying to modify the accepted answer here which seems to leverage RelationalDataReader in order to get back 2 OUTPUT parameters for some calculated dates from an API that the Product Owners wished to be as much database driven as possible.我一直在尝试修改这里接受的答案该答案似乎利用了RelationalDataReader ,以便从产品所有者希望尽可能多的数据库驱动的 API 中获取某些计算日期的 2 个 OUTPUT 参数。

I have a stored procedure that calculates begin and end dates from the SQL queries I have stored in a table so that all they have to to to add another date range is to add it to the table and the rest will fall into place on the UI and the rest of the pipeline.我有一个存储过程,它根据我存储在表中的 SQL 查询计算开始和结束日期,以便他们添加另一个日期范围所要做的就是将它添加到表中,其余的将在 UI 上就位和管道的其余部分。

The first part is the same for the RDFacadeExtensions class but I thought that I could simply add more parameters and get them back, but I seem to be missing something. RDFacadeExtensions 类的第一部分是相同的,但我认为我可以简单地添加更多参数并将它们取回,但我似乎遗漏了一些东西。

var ID = new SqlParameter("ID", 5)
{
    Direction = ParameterDirection.Input,
    DbType = DbType.Int32,
    Size = 500
};

var _beginDate = new SqlParameter("BeginDate", "")
{
    Direction = ParameterDirection.Output,
    DbType = DbType.String,
    Size = 500
};

var _endDate = new SqlParameter("EndDate", "")
{
    Direction = ParameterDirection.Output,
    DbType = DbType.String,
    Size = 500
};

This all seems normal and then I do the following:这一切似乎很正常,然后我执行以下操作:

string begin;
string end;
var sql = $"exec [dbo].[GetDateRange] @ID, @BeginDate OUTPUT, @EndDate OUTPUT";

using (var dr = context.Database.ExecuteSqlQuery(sql, ID, _beginDate, _endDate))
{
    while (dr.DbDataReader.Read())
    {
        var thing = dr.DbDataReader[0].ToString();
    }
    dr.DbDataReader.Close();
    begin = _beginDate.Value.ToString();
    end = _endDate.Value.ToString();
}

Before the end of the using I briefly get the first date but I never seem to be able to get the second date.在使用结束之前,我短暂地获得了第一次约会,但我似乎永远无法获得第二次约会。 I also have empty strings once the datareader is closed.一旦数据读取器关闭,我也有空字符串。

If it matters, my stored procedure is simply this:如果重要的话,我的存储过程就是这样:

    @ID INT,
    @BeginDate DATE OUTPUT,
    @EndDate DATE OUTPUT
BEGIN
    SET NOCOUNT ON;
    DECLARE @Query1 NVARCHAR(1000)
    DECLARE @Query2 NVARCHAR(1000)

    SELECT @Query1 = BeginDate,
           @Query2 = EndDate 
    FROM DateRange 
    WHERE ID = @ID
    
    DECLARE @ParmDefinition1 NVARCHAR(50) = N'@BeginDateOUT DATE OUTPUT';
    DECLARE @ParmDefinition2 NVARCHAR(50) = N'@EndDateOUT DATE OUTPUT';

    EXEC sp_executesql @Query1, @ParmDefinition1, @BeginDateOUT = @BeginDate OUTPUT
    EXEC sp_executesql @Query2, @ParmDefinition2, @EndDateOUT = @EndDate OUTPUT
END

Or perhaps my stored procedure is the issue.或者也许我的存储过程是问题所在。 I get one date but can only hold it for a short time.我有一个约会,但只能保持很短的时间。 The issue with sp_executesql is that it has to go to an OUTPUT parameter and I can't assign the result to anything but an INT variable. sp_executesql 的问题在于它必须转到 OUTPUT 参数,而且除了 INT 变量之外,我不能将结果分配给任何东西。 I need two dates from the stored code in the table.我需要表中存储代码的两个日期。

If there is a way to do that and only return what essentially amounts to a small table result that would be ideal.如果有办法做到这一点,并且只返回本质上相当于一个小表结果的内容,那将是理想的。

Thanks you any that can point me in the right direction.感谢任何可以为我指明正确方向的人。

So, at least in ADO, if you don't close your data reader you will never get back your output parameters.因此,至少在 ADO 中,如果您不关闭数据读取器,您将永远无法取回您的输出参数。 In your sample this should suffice your needs:在您的示例中,这应该足以满足您的需求:

    string begin;
    string end;
    var sql = $"exec [dbo].[GetDateRange] @ID, @BeginDate OUTPUT, @EndDate OUTPUT";

    using (var dr = context.Database.ExecuteSqlQuery(sql, ID, _beginDate, _endDate))
    {
        while (dr.DbDataReader.Read())
        {
            var thing = dr.DbDataReader[0].ToString();
        }
        dr.DbDataReader.Close();
        begin = _beginDate.Value.ToString();
        end = _endDate.Value.ToString();
    }

However there is one interesting solution mentioned in here which seems to tackle exactly your pain point.然而, 这里提到了一个有趣的解决方案,它似乎可以解决您的痛点。

So it would appear I don't need to use sp_executesql nor do I require any OUT parameters.所以看起来我不需要使用 sp_executesql 也不需要任何 OUT 参数。

So I changed the stored procedure to this and minor change to the underlying queries held in the table to remove the select within them to basically allow the construction of the sql within the stored procedure.因此,我将存储过程更改为此,并对表中保存的底层查询进行了细微更改,以删除其中的选择,从而基本上允许在存储过程中构建 sql。

ALTER PROCEDURE [dbo].[GetDateRange] 
@ID INT

AS BEGIN SET NOCOUNT ON; AS BEGIN SET NOCOUNT ON; DECLARE @Query1 NVARCHAR(1000) DECLARE @Query2 NVARCHAR(1000) SELECT @Query1 = BeginDate, @Query2 = EndDate FROM DateRange WHERE ID = @ID DECLARE @Query1 NVARCHAR(1000) DECLARE @Query2 NVARCHAR(1000) SELECT @Query1 = BeginDate,@Query2 = EndDate FROM DateRange WHERE ID = @ID

DECLARE @Test NVARCHAR(1000) = 'SELECT ' + @Query1 + ' AS BeginDate, ' + @Query2 + ' AS EndDate'
EXECUTE (@Test)

END结尾

Then I was able to do the following with the existing code:然后我能够使用现有代码执行以下操作:

        var ID = new SqlParameter("ID", 1)
        {
            Direction = ParameterDirection.Input,
            DbType = DbType.Int32,
            Size = 500
        };

        DataTable dates = new DataTable();
        var sql = $"exec [dbo].[GetDateRange] @ID";

        using (var dr = context.Database.ExecuteSqlQuery(sql, ID))
        {
                dates.Load(dr.DbDataReader);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多个参数传递给 .net core 3.1 上的存储过程 - How to pass multiple parameters to stored procedure on .net core 3.1 EF Core 3.0 存储过程:多个参数 - EF Core 3.0 Stored Procedure : Multiple Parameters 如何使用 EF Core 从存储过程中获取返回值和 output 值? - How to get return values and output values from a stored procedure with EF Core? 如何在 Dot Net Core 3.1 中使用 FromSqlInterpolated/Database.ExecuteSqlInterpolated 执行带输出参数的存储过程? - How to execute stored procedure with Output parameters using FromSqlInterpolated/Database.ExecuteSqlInterpolated in Dot Net Core 3.1? 如何从 EF Core 中的存储过程获取对象列表? - How to get a list of objects from a stored procedure in EF Core? 在 EF Core 3.1 中包含 FromSqlRaw 和存储过程 - Include with FromSqlRaw and stored procedure in EF Core 3.1 使用 EF Core 获取存储过程的输出参数值? - Get output parameter value of a stored procedure using EF Core? 如何在C#中从Web服务的存储过程中获取多个输出参数? - How can I get multiple output parameters from a stored procedure in a web service in C#? 如何使用EF执行带有输入和输出参数的存储过程? - How to execute stored procedure with input and output parameters using EF? EF Core,如何使用输入和输出参数调用存储过程? - EF Core, how to call stored procedure with input and output params?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM