作法:使用與循序結果型式對應的預存程序
這種預存程序可以產生一個以上的結果圖案,但您會知道傳回結果的順序。 將此案例與您不知道傳回順序的案例相比較。 如需詳細資訊,請參閱如何:使用與多個結果型式對應的預存程序。
範例 1
以下是可循序傳回多個結果圖案之預存程序的 T-SQL:
CREATE PROCEDURE MultipleResultTypesSequentially
AS
select * from products
select * from customers
[Function(Name="dbo.MultipleResultTypesSequentially")]
[ResultType(typeof(MultipleResultTypesSequentiallyResult1))]
[ResultType(typeof(MultipleResultTypesSequentiallyResult2))]
public IMultipleResults MultipleResultTypesSequentially()
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
return ((IMultipleResults)(result.ReturnValue));
}
<FunctionAttribute(Name:="dbo.MultipleResultTypesSequentially"), _
ResultType(GetType(MultipleResultTypesSequentiallyResult1)), _
ResultType(GetType(MultipleResultTypesSequentiallyResult2))> _
Public Function MultipleResultTypesSequentially() As IMultipleResults
Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo))
Return CType(result.ReturnValue, IMultipleResults)
End Function
範例 2
您可使用類似下列的程式碼來執行此預存程序。
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
IMultipleResults sprocResults =
db.MultipleResultTypesSequentially();
// First read products.
foreach (Product prod in sprocResults.GetResult<Product>())
{
Console.WriteLine(prod.ProductID);
}
// Next read customers.
foreach (Customer cust in sprocResults.GetResult<Customer>())
{
Console.WriteLine(cust.CustomerID);
}
Dim db As New Northwnd("c:\northwnd.mdf")
Dim sprocResults As IMultipleResults = _
db.MultipleResultTypesSequentially
' First read products.
For Each prod As Product In sprocResults.GetResult(Of Product)()
Console.WriteLine(prod.ProductID)
Next
' Next read customers.
For Each cust As Customer In sprocResults.GetResult(Of Customer)()
Console.WriteLine(cust.CustomerID)
Next