HOW TO:執行參數化查詢 (Entity Framework)
本主題示範如何使用 ObjectQuery 來執行含有參數的 實體 SQL 查詢。此範例會將兩個參數傳遞給 ObjectQuery、執行查詢,然後逐一查看 Contact
項目的集合。此外,也會顯示使用下列每一個 實體架構 查詢技術的相同範例:
使用 ObjectQuery<T> 的 Entity SQL
ObjectQuery<T> 的查詢產生器方法
本主題的範例是根據 AdventureWorks Sales Model (EDM)。若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 實體架構。若要這樣做,請完成 HOW TO:手動設定 Entity Framework 專案和 HOW TO:以手動方式定義 Entity Data Model (Entity Framework) 中的程序。您也可以使用 [Entity Data Model 精靈] 定義 AdventureWorks Sales Model。如需詳細資訊,請參閱 HOW TO:使用 Entity Data Model 精靈 (Entity Framework)。
範例
以下是 實體 SQL 範例。
Using advWorksContext As New AdventureWorksEntities
Try
' Create a query that takes two parameters.
Dim queryString As String = "SELECT VALUE Contact FROM AdventureWorksEntities.Contact " & _
"AS Contact WHERE Contact.LastName = @ln AND " & _
"Contact.FirstName = @fn"
' Add parameters to the collection.
Dim contactQuery As New ObjectQuery(Of Contact)(queryString, advWorksContext, MergeOption.NoTracking)
contactQuery.Parameters.Add(New ObjectParameter("ln", "Adams"))
contactQuery.Parameters.Add(New ObjectParameter("fn", "Frances"))
' Iterate through the collection of Contact items.
For Each result As Contact In contactQuery
Console.WriteLine("Last Name: {0} First Name: {1}", _
result.LastName, result.FirstName)
Next
Catch ex As EntityException
Console.WriteLine(ex.ToString)
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
try
{
// Create a query that takes two parameters.
string queryString =
@"SELECT VALUE Contact FROM AdventureWorksEntities.Contact
AS Contact WHERE Contact.LastName = @ln AND
Contact.FirstName = @fn";
ObjectQuery<Contact> contactQuery =
new ObjectQuery<Contact>(queryString, advWorksContext);
// Add parameters to the collection.
contactQuery.Parameters.Add(new ObjectParameter("ln", "Adams"));
contactQuery.Parameters.Add(new ObjectParameter("fn", "Frances"));
// Iterate through the collection of Contact items.
foreach (Contact result in contactQuery)
Console.WriteLine("Last Name: {0}; First Name: {1}",
result.LastName, result.FirstName);
}
catch (EntityException ex)
{
Console.WriteLine(ex.ToString());
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
}
這是查詢產生器方法範例。
Dim firstName As String = "Frances"
Dim lastName As String = "Adams"
Using context As New AdventureWorksEntities
Try
' Get the contacts with the specified name.
Dim contactQuery As ObjectQuery(Of Contact) = _
context.Contact _
.Where("it.LastName = @ln AND it.FirstName = @fn", _
New ObjectParameter("ln", lastName), _
New ObjectParameter("fn", firstName))
' Iterate through the collection of Contact items.
For Each result As Contact In contactQuery
Console.WriteLine("Last Name:{0}First Name: {1}", _
result.LastName, result.FirstName)
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
string firstName = @"Frances";
string lastName = @"Adams";
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Get the contacts with the specified name.
ObjectQuery<Contact> contactQuery = context.Contact
.Where("it.LastName = @ln AND it.FirstName = @fn",
new ObjectParameter("ln", lastName),
new ObjectParameter("fn", firstName));
// Iterate through the collection of Contact items.
foreach (Contact result in contactQuery)
Console.WriteLine("Last Name: {0}; First Name: {1}",
result.LastName, result.FirstName);
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
另請參閱
工作
HOW TO:執行傳回實體類型的查詢 (Entity Framework)
HOW TO:執行傳回匿名型別的查詢 (Entity Framework)
HOW TO:執行傳回基本型別的查詢 (Entity Framework)