我在SQL Server 2016中有一个存储过程,我希望使用Entity Framework Core v1.1.1从ASP.Net Core项目中的方法调用。
这是我的方法......
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;
}
}
这是我的存储过程...
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
当我从SSMS 2016运行我的存储过程时,我得到了所需的字符串结果(即00000123460),但上面列出的GetNextSerialNumber方法中的result值为-1。
有什么想法吗?
提前感谢您提供的任何帮助。
答案 0 :(得分:2)
context.Database.ExecuteSqlCommand(
"exec GetNextSerialNumber @serialNumber OUT", serialNumber);
ExecuteSqlCommand
仅返回执行结果(' s -1)。要获取序列号,只需在执行命令后检查输出参数值:
var val = serialNumber.Value;