简体   繁体   English

如何从Database.ExecuteSqlCommand调用的存储过程中获取字符串结果

[英]How can I get a string result from a stored procedure called by Database.ExecuteSqlCommand

I have a stored procedure in SQL Server 2016 that I want to call from a method in an ASP.Net Core project using Entity Framework Core v1.1.1. 我在SQL Server 2016中有一个存储过程,我想使用Entity Framework Core v1.1.1从ASP.Net Core项目中的方法调用该存储过程。

Here is my method... 这是我的方法

    private string GetNextSerialNumber(string a_mailerId, string a_serviceTypeCode)
    {
        try
        {
            var mailerId = new SqlParameter
            {
                ParameterName = "@mailerId",
                SqlDbType = SqlDbType.NVarChar,
                Direction = ParameterDirection.Input,
                Size = 9,
                Value = a_mailerId
            };

            var serviceTypeCode = new SqlParameter
            {
                ParameterName = "@serviceTypeCode",
                SqlDbType = SqlDbType.NVarChar,
                Direction = ParameterDirection.Input,
                Size = 4,
                Value = a_serviceTypeCode
            };

            var serialNumber = new SqlParameter
            {
                ParameterName = "@serialNumber",
                SqlDbType = SqlDbType.NVarChar,
                Size = 14,
                Direction = ParameterDirection.Output
            };

            var result = m_mailpieceRoContext.Database.ExecuteSqlCommand(
                "exec GetNextSerialNumber @mailerId, @serviceTypeCode, @serialNumber OUT", mailerId, serviceTypeCode, serialNumber);


            return result;

        }
        catch (Exception ex)
        {

            throw;
        }

    }

and here is my stored procedure... 这是我的存储过程...

ALTER PROCEDURE [dbo].[GetNextSerialNumber]
    @mailerId               NVARCHAR(9),
    @serviceTypeCode        NVARCHAR(4),
    @serialNumber           NVARCHAR(14)    OUTPUT
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @counter        BIGINT
    DECLARE @start_counter  BIGINT
    DECLARE @end_counter    BIGINT
    DECLARE @length         INT
    DECLARE @tempCounters   TABLE
    (
        current_counter     BIGINT,
        start_counter       BIGINT,
        end_counter         BIGINT,
        [length]            INT
    )

    INSERT INTO
        @TempCounters
    SELECT 
        current_counter,
        start_counter,
        end_counter,
        [length]
    FROM 
        MailerIdCounters
    WHERE 
        mailer_id = @mailerId
        AND service_type_code = @serviceTypeCode
        AND active = 1


    IF EXISTS (SELECT * FROM @tempCounters)
    BEGIN
        SELECT @counter = current_counter FROM @tempCounters
        SELECT @start_counter = start_counter FROM @tempCounters
        SELECT @end_counter = end_counter FROM @tempCounters
        SELECT @length = [length] FROM @tempCounters
    END

    IF (@counter + 1) > @end_counter
        SET @counter = @start_counter
    ELSE 
        SET @counter = @Counter  + 1

    UPDATE MailerIDCounters
    SET current_counter = @counter
    WHERE mailer_id = @mailerId
    AND service_type_code = @serviceTypeCode

    SELECT RIGHT('00000000000000'+CAST(@counter AS VARCHAR(14)), @length)
    SET @serialNumber = @counter
END   

When I run my stored procedure from SSMS 2016, I get the desired string result (ie 00000123460) but the value of result in the GetNextSerialNumber method listed above is -1. 当我从SSMS 2016运行存储过程时,我得到了所需的字符串结果(即00000123460),但是上面列出的GetNextSerialNumber方法中的result值为-1。

Any ideas? 有任何想法吗?

Thanks in advance for any help you can provide. 在此先感谢您提供的任何帮助。

context.Database.ExecuteSqlCommand(
   "exec GetNextSerialNumber @serialNumber OUT", serialNumber);

ExecuteSqlCommand returns execution result only (it's -1). ExecuteSqlCommand仅返回执行结果(它为-1)。 To get serial number, just check the output parameter value after the command has been executed: 要获取序列号,只需在执行命令后检查输出参数值即可:

var val = serialNumber.Value;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM