简体   繁体   English

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

[英]SQL Server Output Parameter showing null value in EF C#

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服务器上的完美工作见下面的输出:

I am calling this stored procedure through Entity Framework : 我通过Entity Framework调用此stored procedure

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. 请帮忙。

Not sure whether it makes a difference, but did you try to use "typeof(Int32)" instead of "typeof(int)"? 不确定它是否有所作为,但你是否尝试使用“typeof(Int32)”而不是“typeof(int)”? Also try NewMessage as ObjectParameter and not as var, maybe it makes a difference. 也可以尝试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? 您是否尝试在没有参数的情况下运行存储过程,意味着无论您输入什么参数,都会返回带有值的@ Count-variable? Like this you can identify whether your input parameters are wrong (handed over by C#) or not. 像这样,您可以识别输入参数是否错误(由C#移交)。

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

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