2016-04-27 75 views
0

我有一個帶輸出參數的下面的存儲過程。在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值。

請幫忙。

+0

,它在SQL Management Studio中的作品? – jamiedanq

+0

使用'SELECT @ Count'是多餘的。您不輸入'out'參數的值,因爲您輸入'null'。 –

+0

它使用存儲過程旁邊的FirstOrDefault()來解決。 謝謝你們的幫助。 –

回答

1

不知道它是否有所作爲,但您是否嘗試使用「typeof(Int32)」而不是「typeof(int)」?也可以嘗試NewMessage作爲ObjectParameter,而不是var,也許它有所作爲。

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

您是否嘗試運行沒有參數的存儲過程,意味着無論您輸入什麼參數都返回@ Count-variable值?像這樣,您可以識別您的輸入參數是否錯誤(由C#移交)或不。