0 out of 2 rated this helpful - Rate this topic

Customizing Operations by Using Stored Procedures Exclusively (LINQ to SQL)

Access to data by using only stored procedures is a common scenario.

Description

You can modify the example provided in Customizing Operations By Using Stored Procedures (LINQ to SQL) by replacing even the first query (which causes dynamic SQL execution) with a method call that wraps a stored procedure.

Assume CustomersByCity is the method, as in the following example.

Code


[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));
}


The following code executes without any dynamic 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();


Did you find this helpful?
(1500 characters remaining)