在EF C#中显示空值的SQL Server输出参数#
问题描述:
我有一个带输出参数的下面的存储过程。在EF C#中显示空值的SQL Server输出参数#
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 [email protected] and fdIsConsumerType=1 and fdIsReadatAdmin=0)
else
select @Count=(Select count(fdId) from tb_CRS_Messages where [email protected] and fdIsMemberType=1 and fdIsReadatAdmin=0)
end
else
begin
if @type='consumer'
select @Count=(Select count(fdId) from tb_CRS_Messages where [email protected] and fdIsConsumerType=1 and fdIsReadatFront=0)
else
select @Count=(Select count(fdId) from tb_CRS_Messages where [email protected] and fdIsMemberType=1 and fdIsReadatFront=0)
END
SELECT @Count
END
这是在SQL服务器完美的工作见下面的输出:
我打电话这个stored procedure
通过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; }
}
它使输出参数null
值。
请帮忙。
答
不知道它是否有所作为,但您是否尝试使用“typeof(Int32)”而不是“typeof(int)”?也可以尝试NewMessage作为ObjectParameter,而不是var,也许它有所作为。
ObjectParameter NewMessage = new ObjectParameter("NewMessage ", typeof(Int32));
您是否尝试运行没有参数的存储过程,意味着无论您输入什么参数都返回@ Count-variable值?像这样,您可以识别您的输入参数是否错误(由C#移交)或不。
,它在SQL Management Studio中的作品? – jamiedanq
使用'SELECT @ Count'是多余的。您不输入'out'参数的值,因为您输入'null'。 –
它使用存储过程旁边的FirstOrDefault()来解决。 谢谢你们的帮助。 –