简体   繁体   中英

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.

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.

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). To get serial number, just check the output parameter value after the command has been executed:

var val = serialNumber.Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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