如何:返回行集
此示例从数据库中返回行集合,并包含用于筛选结果的输入参数。
当你执行返回行集合的存储过程时,会用到结果类,它存储从存储过程中返回的结果。 有关详细信息,请参阅分析 LINQ to SQL 源代码。
示例
下面的示例表示一个存储过程,该存储过程返回客户行并使用输入参数来仅返回将“London”列为客户城市的那些行。 该示例假定有一个可枚举的 CustomersByCityResult
类。
CREATE PROCEDURE [dbo].[Customers By City]
(@param1 NVARCHAR(20))
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT CustomerID, ContactName, CompanyName, City from Customers
as c where c.City=@param1
END
[Function(Name="dbo.Customers By City")]
public ISingleResult<CustomersByCityResult> CustomersByCity([Parameter(DbType="NVarChar(20)")] string param1)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), param1);
return ((ISingleResult<CustomersByCityResult>)(result.ReturnValue));
}
// Call the stored procedure.
void ReturnRowset()
{
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
ISingleResult<CustomersByCityResult> result =
db.CustomersByCity("London");
foreach (CustomersByCityResult cust in result)
{
Console.WriteLine("CustID={0}; City={1}", cust.CustomerID,
cust.City);
}
}
<FunctionAttribute(Name:="dbo.Customers By City")> _
Public Function CustomersByCity(<Parameter(DbType:="NVarChar(20)")> ByVal param1 As String) As ISingleResult(Of CustomersByCityResult)
Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), param1)
Return CType(result.ReturnValue, ISingleResult(Of CustomersByCityResult))
End Function
Sub ReturnRowset()
' Call the stored procedure.
Dim db As New Northwnd("c:\northwnd.mdf")
Dim result As IEnumerable(Of CustomersByCityResult) = _
db.CustomersByCity("London")
For Each cust As CustomersByCityResult In result
Console.WriteLine("CustID={0}; City={1}", _
cust.CustomerID, cust.City)
Next
End Sub