Share via


방법: 매개 변수를 받는 저장 프로시저 사용(LINQ to SQL)

업데이트: November 2007

LINQ to SQL에서는 출력 매개 변수를 참조 매개 변수에 매핑하고 값 형식에 대해 매개 변수를 nullable로 선언합니다.

행 집합을 반환하는 쿼리에 입력 매개 변수를 사용하는 방법에 대한 예제는 방법: 저장 프로시저를 사용하여 행 집합 반환(LINQ to SQL)을 참조하십시오.

예제

다음 예제에서는 단일 입력 매개 변수(고객 ID)를 사용하여 출력 매개 변수(해당 고객의 총 판매액)를 반환합니다.

CREATE PROCEDURE [dbo].[CustOrderTotal] 
@CustomerID nchar(5),
@TotalSales money OUTPUT
AS
SELECT @TotalSales = SUM(OD.UNITPRICE*(1-OD.DISCOUNT) * OD.QUANTITY)
FROM ORDERS O, "ORDER DETAILS" OD
where O.CUSTOMERID = @CustomerID AND O.ORDERID = OD.ORDERID
<FunctionAttribute(Name:="dbo.CustOrderTotal")> _
 Public Function CustOrderTotal(<Parameter(Name:="CustomerID", DbType:="NChar(5)")> ByVal customerID As String, <Parameter(Name:="TotalSales", DbType:="Money")> ByRef totalSales As System.Nullable(Of Decimal)) As <Parameter(DbType:="Int")> Integer
    Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), customerID, totalSales)
    totalSales = CType(result.GetParameterValue(1), System.Nullable(Of Decimal))
    Return CType(result.ReturnValue, Integer)
End Function
    [Function(Name="dbo.CustOrderTotal")]
    [return: Parameter(DbType="Int")]
    public int CustOrderTotal([Parameter(Name="CustomerID", DbType="NChar(5)")] string customerID, [Parameter(Name="TotalSales", DbType="Money")] ref System.Nullable<decimal> totalSales)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID, totalSales);
        totalSales = ((System.Nullable<decimal>)(result.GetParameterValue(1)));
        return ((int)(result.ReturnValue));
    }

이 저장 프로시저는 다음과 같이 호출할 수 있습니다.

Dim db As New Northwnd("C:\...\northwnd.mdf")
Dim totalSales As Decimal? = 0
db.CustOrderTotal("alfki", totalSales)

Console.WriteLine(totalSales)
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
decimal? totalSales = 0;
db.CustOrderTotal("alfki", ref totalSales);

Console.WriteLine(totalSales);

참고 항목

개념

샘플 데이터베이스 다운로드(LINQ to SQL)

Nullable 값 형식

참조

Nullable 형식 사용(C# 프로그래밍 가이드)

기타 리소스

저장 프로시저(LINQ to SQL)