SQL Server输出参数在EF C#中显示空值

时间:2022-04-11 02:18:09

I have a below stored procedure with output parameter.

我有一个带有输出参数的下面存储过程。

ALTER proc [dbo].[CRS_GetNewMessageCount]
@CaseId int,
@Type varchar(50),
@Location varchar(50),
@Count int out
as

begin
if @location='admin'
    begin
    if @type='consumer'
    select @Count=(Select count(fdId) from tb_CRS_Messages where fdCaseId=@CaseId and fdIsConsumerType=1 and fdIsReadatAdmin=0)
    else
    select @Count=(Select count(fdId) from tb_CRS_Messages where fdCaseId=@CaseId and fdIsMemberType=1 and fdIsReadatAdmin=0)
    end
else
begin
    if @type='consumer'
    select @Count=(Select count(fdId) from tb_CRS_Messages where fdCaseId=@CaseId and fdIsConsumerType=1 and fdIsReadatFront=0)
else
select @Count=(Select count(fdId) from tb_CRS_Messages where fdCaseId=@CaseId and fdIsMemberType=1 and fdIsReadatFront=0)
END
SELECT @Count
END

It is perfect working at SQL server see below output :

它在SQL服务器上的完美工作见下面的输出:

SQL Server输出参数在EF C#中显示空值

I am calling this stored procedure through Entity Framework :

我通过实体框架调用此存储过程:

using (DbContext db = new DbContext())
            {
                try
                {
                    var NewMessage = new ObjectParameter("Count", typeof(int));
                    int returnValue = 0;
                    db.CRS_GetNewMessageCount(CaseId, type, location, NewMessage);
                    int.TryParse(NewMessage.Value.ToString(), out returnValue);
                    return returnValue;
                }
                catch { return 0; }
            }

It gives null value in output parameter.

它在输出参数中给出null值。

Please help.

请帮忙。

2 个解决方案

#1


1  

Not sure whether it makes a difference, but did you try to use "typeof(Int32)" instead of "typeof(int)"? Also try NewMessage as ObjectParameter and not as var, maybe it makes a difference.

不确定它是否有所作为,但你是否尝试使用“typeof(Int32)”而不是“typeof(int)”?也可以尝试NewMessage作为ObjectParameter而不是var,也许它会有所不同。

ObjectParameter NewMessage = new ObjectParameter("NewMessage ", typeof(Int32));

Did you try to run your stored procedure without parameters, means return @Count-variable with a value no matter what parameters you enter? Like this you can identify whether your input parameters are wrong (handed over by C#) or not.

您是否尝试在没有参数的情况下运行存储过程,意味着无论您输入什么参数,都会返回带有值的@ Count-variable?像这样,您可以识别输入参数是否错误(由C#移交)。

#2


-1  

Hope this helps How to: Execute a Query Using a Stored Procedure with In and Out Parameters

希望这有助于如何:使用带有输入和输出参数的存储过程执行查询

https://msdn.microsoft.com/en-us/library/bb896334(v=vs.100).aspx

https://msdn.microsoft.com/en-us/library/bb896334(v=vs.100).aspx

#1


1  

Not sure whether it makes a difference, but did you try to use "typeof(Int32)" instead of "typeof(int)"? Also try NewMessage as ObjectParameter and not as var, maybe it makes a difference.

不确定它是否有所作为,但你是否尝试使用“typeof(Int32)”而不是“typeof(int)”?也可以尝试NewMessage作为ObjectParameter而不是var,也许它会有所不同。

ObjectParameter NewMessage = new ObjectParameter("NewMessage ", typeof(Int32));

Did you try to run your stored procedure without parameters, means return @Count-variable with a value no matter what parameters you enter? Like this you can identify whether your input parameters are wrong (handed over by C#) or not.

您是否尝试在没有参数的情况下运行存储过程,意味着无论您输入什么参数,都会返回带有值的@ Count-variable?像这样,您可以识别输入参数是否错误(由C#移交)。

#2


-1  

Hope this helps How to: Execute a Query Using a Stored Procedure with In and Out Parameters

希望这有助于如何:使用带有输入和输出参数的存储过程执行查询

https://msdn.microsoft.com/en-us/library/bb896334(v=vs.100).aspx

https://msdn.microsoft.com/en-us/library/bb896334(v=vs.100).aspx