Share via


如何:使用针对顺序结果形状映射的存储过程

这种存储过程可以生成多个结果形状,但您知道结果的返回顺序。 请将此方案与您不知道返回顺序的方案作一个对比。 有关详细信息,请参阅如何:使用针对多个结果形状映射的存储过程

示例 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

请参阅