Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
L'accesso ai dati usando solo stored procedure è uno scenario comune.
Esempio
Descrizione
È possibile modificare l'esempio fornito in Personalizzazione delle operazioni tramite stored procedure sostituendo anche la prima query (che causa l'esecuzione dinamica di SQL) con una chiamata al metodo che esegue il wrapping di una stored procedure.
Si supponga CustomersByCity che sia il metodo , come nell'esempio seguente.
Codice
[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
Il codice seguente viene eseguito senza sql dinamico.
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