1

C#代碼檢索存儲的過程輸出參數在實體框架總是空

public List<searchProducts_Result> GetProducts() 
{ 
      var nameParam = new ObjectParameter("numbers", typeof(int)); 
      List<searchProducts_Result> listobjs = new List<searchProducts_Result>(); 
      ObjectResult<searchProducts_Result> objResult = null; 
      searchProducts_Result outParam = new searchProducts_Result(); 
      using (var db = new SPWebEntities()) 
      { 
       objResult = db.searchProducts("asd", 2, 5, "15", nameParam); 
       if (nameParam.Value != null) 
        outParam.UserID = nameParam.Value.ToString(); 
       else 
        outParam.UserID = "0"; 
       listobjs.Add(outParam); 
       foreach (searchProducts_Result sr in objResult) 
       { 
        listobjs.Add(sr); 
       } 
      } 

      return listobjs; 
} 

我的存儲過程:

[searchProducts] 
    @productName varchar(50), 
    @pageStart int=2, 
    @pageEnd int=4, 
    @result varchar(MAX), 
    @numbers int output 
as 
    select @numbers = COUNT(*) 
    from products 
    where productName like @productName 

    select * 
    from (select 
      *, 
      ROW_NUMBER() over (order by ID) as row 
     from products 
     where productName like @productName) a 
    where a.row between @pageStart and @pageEnd 

nameParam.Value它總是返回空值

當我執行存儲過程SQL Server Mgmt Studio,它似乎有一個值,但在C#中它始終爲空

+0

什麼是搜索產品的方法? – Botonomous

+0

通過函數導入從(ado.net實體框架)生成的搜索產品方法導入 – DevC

+0

您嘗試使用db.sqlquery 執行過程嗎? – Botonomous

回答

1

您必須枚舉結果才能真正執行存儲過程。試試這個:

public List<searchProducts_Result> GetProducts() 
{ 
    var nameParam = new ObjectParameter("numbers", typeof(int)); 
    List<searchProducts_Result> listobjs = new List<searchProducts_Result>(); 

    // List<searchProducts_Result> objResult = null; 

    searchProducts_Result outParam = new searchProducts_Result(); 
    using (var db = new SPWebEntities()) 
    { 
     // by calling ToList() you execute the SP 
     List<searchProducts_Result> objResult = 
      db.searchProducts("asd", 2, 5, "15", nameParam).ToList(); 

     if (nameParam.Value != null) 
     outParam.UserID = nameParam.Value.ToString(); 
     else 
     outParam.UserID = "0"; 
     listobjs.Add(outParam); 

     foreach (searchProducts_Result sr in objResult) 
     { 
     listobjs.Add(sr); 
     } 
    } 

    return listobjs; 
}