C# SQL Server输出参数在EF C中显示空值#
我有一个下面带有输出参数的存储过程C# SQL Server输出参数在EF C中显示空值#,c#,sql,asp.net-mvc,entity-framework,output-parameter,C#,Sql,Asp.net Mvc,Entity Framework,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_Messa
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
在SQL server上工作非常完美,请参见以下输出:
我通过实体框架
调用这个存储过程
:
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; }
}
它在输出参数中给出null
值
请提供帮助。不确定这是否有区别,但您是否尝试使用“typeof(Int32)”而不是“typeof(int)”?还可以尝试将NewMessage作为ObjectParameter而不是var,也许这会有所不同
ObjectParameter NewMessage = new ObjectParameter("NewMessage ", typeof(Int32));
您是否尝试在没有参数的情况下运行存储过程,即无论输入什么参数,都返回带有值的@Count变量?这样,您就可以确定输入参数是否错误(由C#移交)。不确定这是否有区别,但您是否尝试使用“typeof(Int32)”而不是“typeof(int)”?还可以尝试将NewMessage作为ObjectParameter而不是var,也许这会有所不同
ObjectParameter NewMessage = new ObjectParameter("NewMessage ", typeof(Int32));
您是否尝试在没有参数的情况下运行存储过程,即无论输入什么参数,都返回带有值的@Count变量?这样,您可以确定输入参数是否错误(由C#移交)。希望这有帮助
希望这有帮助
它在sql management studio中工作?使用
SELECT@Count
是多余的。您没有捕获out
参数的值,因为您输入了null
。它是使用存储过程旁边的FirstOrDefault()解决的。谢谢你们的帮助。它在sql management studio中工作吗?使用SELECT@Count
是多余的。您没有捕获out
参数的值,因为您输入了null
。它是使用存储过程旁边的FirstOrDefault()解决的。谢谢你们的帮助。