儲存程序與 UDF 差異:SP 支援多結果集、僅回傳 int、支援 Output 參數;UDF 單結果集、可回傳標量/表值、支援內聯查詢。
對應 ISingleResult<TResult>,非延遲執行,可接 LINQ to Objects 操作(如 OrderBy)。可在 OR Designer 變更結果類別,但需注意型別相容。
SP 多個 SELECT 需手動修改 DBML/代碼:
IMultipleResults。[ResultType(typeof(T))] 屬性,順序對應 SELECT 順序。呼叫端依序 GetResult<T1>()、GetResult<T2>(),超過結果集數量會拋例外。
對應 ref 參數,內部透過 result.GetParameterValue(index) 取值(索引從 0 起)。
LINQ 預設忽略 SP Return Value,需自行修改代碼取得:
(int)result.ReturnValue。out int returnValue,或修改 GetParameterValue 索引(最後一個參數索引+1 為 Return Value)。[Function(Name="dbo.CustOrderTotal")]
public int CustOrderTotal(string customerID, ref decimal? totalSales) {
var result = this.ExecuteMethodCall(this, methodInfo, customerID, totalSales);
totalSales = (decimal?)result.GetParameterValue(1);
return (int)result.ReturnValue;
}