2010-08-18 379 views
3

當您執行存儲過程時,是否可以從LINQ To SQL DataContext獲取輸出參數?從LINQ中存儲過程捕獲輸出參數到SQL ExecuteQuery

IEnumerable<Address> result = 
    ExecuteQuery<Address>(((MethodInfo)(MethodInfo.GetCurrentMethod())), 
     address, pageIndex, pageSize, totalCount); 

其中addresspageIndexpageSize是輸入參數,和TotalCount是一個輸出參數。

如何捕獲輸出參數?

這裏是在做另一次嘗試卻又無法得到的參數值:


    [Function(Name = "Telecom.AddressSearch")] 
     private IEnumerable SearchAddress([Parameter(Name = "address", DbType = "varchar")] string address, 
              [Parameter(Name = "pageIndex", DbType = "int")] int pageIndex, 
              [Parameter(Name = "pageSize", DbType = "int")] int pageSize, 
              [Parameter(Name = "totalCount", DbType = "int")] ref int totalCount) 
     {    
      IEnumerable result = ExecuteQuery(((MethodInfo)(MethodInfo.GetCurrentMethod())), address, pageIndex, pageSize, totalCount); 

      return result; 
     }

回答

6

斯科特·格思裏有一個blog post描述如何得到的LINQ to SQL存儲過程的工作。雖然他的文章並不直接回答你的問題,但它提供了一些可能有用的線索。在.dbml類中定義方法時,「LINQ to SQL將SPROC中的參數映射爲參考參數(ref關鍵字)」。您可以嘗試使用ref關鍵字並查看totalCount是否得到更新。

IEnumerable r = ExecuteQuery(((MethodInfo)(MethodInfo.GetCurrentMethod())), 
    address, pageIndex, pageSize, ref totalCount); 

更新:

我做了一些調查研究,並發現一種方式,應該爲你工作。這是來自MSDN article。你需要擴展你的DataContext,但這不應該是一個大問題(看起來你可能已經在這樣做)。請查看文章以獲得更多信息,但基本部分是這樣的:

[Function(Name="dbo.CustOrderTotal")] 
[return: Parameter(DbType="Int")] 
public int CustOrderTotal([Parameter(Name="CustomerID", DbType="NChar(5)")] string customerID, [Parameter(Name="TotalSales", DbType="Money")] ref System.Nullable<decimal> totalSales) 
{ 
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID, totalSales); 
    totalSales = ((System.Nullable<decimal>)(result.GetParameterValue(1))); 
    return ((int)(result.ReturnValue)); 
} 

其中'this'是您的DataContext。

更新2:

的IExecuteResult有ReturnValue屬性。它是Object類型的,所以你必須將它轉換成結果,但它應該允許你獲得結果的Enumerable。在你的情況,這樣的事情應該工作:

IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), address, pageIndex, pageSize, totalCount); 
totalCount = ((System.Nullable<decimal>)(result.GetParameterValue(3))); 
return (IEnumerable<Address>)result.ReturnValue; 
+0

我已經試過了,但它說,PARAMS []對象已經找到另一種可能的解決方案無效參數 – BBurke 2010-08-18 16:02:45

+0

,檢查我的更新答案。 – sgriffinusa 2010-08-19 01:17:59

+0

是的,我看了看,但我不認爲它會工作。 我看不到IExecuteResult將如何返回Enumerable數據 – BBurke 2010-08-19 09:56:35

相關問題