다음을 통해 공유


저장 프로시저를 단독으로 사용하여 작업 사용자 지정

저장 프로시저만 사용하여 데이터에 액세스하는 것이 일반적인 시나리오입니다.

예시

설명

제공된 예제에서 첫 번째 쿼리(동적 SQL 실행을 발생시키는 쿼리)를 저장 프로시저를 래핑하는 메서드 호출로 바꿔서, 저장 프로시저를 사용하여 사용자 지정 작업하는 방식을 수정할 수 있습니다.

다음 예제와 같이 메서드라고 가정 CustomersByCity 합니다.

코드

[Function()]
public IEnumerable<Customer> CustomersByCity(
    [Parameter(Name = "City", DbType = "NVarChar(15)")]
    string city)
{
    IExecuteResult result = this.ExecuteMethodCall(this,
        ((MethodInfo)(MethodInfo.GetCurrentMethod())),
        city);
    return ((IEnumerable<Customer>)(result.ReturnValue));
}
<[Function]()> _
Public Function CustomersByCity( _
    <Parameter(Name:="City", DbType:="NVarChar(15)")> ByVal _
        city As String) As IEnumerable(Of Customer)

    Dim result = Me.ExecuteMethodCall(Me, _
        (CType(MethodInfo.GetCurrentMethod(), IEnumerable(Of  _
        Customer))), city)
    Return CType(result.ReturnValue, IEnumerable(Of Customer))
End Function

다음 코드는 동적 SQL 없이 실행됩니다.

NorthwindThroughSprocs db = new NorthwindThroughSprocs("...");
// Use a method call (stored procedure wrapper) instead of
// a LINQ query against the database.
var custQuery =
    db.CustomersByCity("London");

foreach (Customer custObj in custQuery)
{
    // Deferred loading of custObj.Orders uses the override
    // LoadOrders. There is no dynamic SQL.
    foreach (Order ord in custObj.Orders)
    {
        // Make some changes to customers/orders.
        // Overrides for Customer are called during the execution
        // of the following.
    }
}
db.SubmitChanges();
Dim db As New Northwind("...")
' Use a method call (stored procedure wrapper) instead of
' a LINQ query against the database.
Dim custQuery = db.CustomersByCity("London")

For Each custObj In custQuery
    ' Deferred loading of custObj.Orders uses the override
    ' LoadOrders. There is no dynamic SQL.

    For Each ord In custObj.Orders
        ' Make some changes to customers/orders.
        ' Overrides for Customer are called during the execution
        ' of the following:
        db.SubmitChanges()
    Next
Next

참고하십시오