Documentation
-
Learn more about: Stored Procedures
-
How to: Use Stored Procedures that Take Parameters - ADO.NET
Learn more about: How to: Use Stored Procedures that Take Parameters
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This example returns a rowset from the database, and includes an input parameter to filter the result.
When you execute a stored procedure that returns a rowset, you use a result class that stores the returns from the stored procedure. For more information, see Analyzing LINQ to SQL Source Code.
The following example represents a stored procedure that returns rows of customers and uses an input parameter to return only those rows that list "London" as the customer city. The example assumes an enumerable CustomersByCityResult
class.
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={cust.CustomerID}; City={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
Documentation
Learn more about: Stored Procedures
How to: Use Stored Procedures that Take Parameters - ADO.NET
Learn more about: How to: Use Stored Procedures that Take Parameters
Training
Module
Create stored procedures and user-defined functions - Training
This content is a part of Create stored procedures and user-defined functions.