DataContext.ExecuteQuery 方法

定義

多載

ExecuteQuery(Type, String, Object[])

直接在資料庫上執行 SQL 查詢。Executes SQL queries directly on the database.

ExecuteQuery<TResult>(String, Object[])

直接在資料庫上執行 SQL 查詢,並傳回物件。Executes SQL queries directly on the database and returns objects.

ExecuteQuery(Type, String, Object[])

直接在資料庫上執行 SQL 查詢。Executes SQL queries directly on the database.

public:
 System::Collections::IEnumerable ^ ExecuteQuery(Type ^ elementType, System::String ^ query, ... cli::array <System::Object ^> ^ parameters);
public System.Collections.IEnumerable ExecuteQuery (Type elementType, string query, params object[] parameters);
member this.ExecuteQuery : Type * string * obj[] -> System.Collections.IEnumerable
Public Function ExecuteQuery (elementType As Type, query As String, ParamArray parameters As Object()) As IEnumerable

參數

elementType
Type

要傳回的 IEnumerable<T> 型別。The type of the IEnumerable<T> to be returned.

查詢結果中的資料行與物件中的欄位或屬性相符合的演算法用法如下:The algorithm for matching columns in the result of the query to fields or properties in the object works as follows: 如果欄位或屬性已對應至特定資料行名稱,結果集 (Resultset) 必須包含該資料行名稱。If a field or property is mapped to a particular column name, that column name is expected in the resultset.

如果欄位或屬性未對應,結果集必須包含與該欄位或屬性同名的資料行。If a field or property is not mapped, a column with the same name as the field or property is expected in the resultset.

尋找區分大小寫的相符項時會先進行比較。The comparison is performed by looking for a case-sensitive match first. 如果找不到這種相符項,就會繼續搜尋不區分大小寫的相符項。If this match is not found, a subsequent search occurs for a case-insensitive match.

當下列所有條件皆為 true 時,查詢必須傳回該物件所有追蹤的欄位和屬性 (會延後載入的欄位和屬性除外):The query must return all the tracked fields and properties of the object (except those that are loaded on a deferred basis) when all the following conditions are true: TDataContext 明確追蹤的實體。T is an entity explicitly tracked by the DataContext.

ObjectTrackingEnabledtrueObjectTrackingEnabled is true.

這個實體具有主索引鍵。The entity has a primary key.

否則,會擲回例外狀況。Otherwise an exception is thrown.

query
String

要執行的 SQL 查詢。The SQL query to be executed.

parameters
Object[]

要傳遞至命令的參數陣列。The array of parameters to be passed to the command. 請注意下列情況:Note the following behavior: 如果陣列中的物件數目少於命令字串中所識別的最高數目,便會擲回例外狀況。If the number of objects in the array is less than the highest number identified in the command string, an exception is thrown.

如果陣列包含命令字串中未參考的物件,則不會擲回任何例外狀況。If the array contains objects that are not referenced in the command string, no exception is thrown.

如果參數為 null,則會轉換成 DBNull.ValueIf a parameter is null, it is converted to DBNull.Value.

傳回

物件的 IEnumerable<T> 集合,由查詢傳回。An IEnumerable<T> collection of objects returned by the query.

ExecuteQuery<TResult>(String, Object[])

直接在資料庫上執行 SQL 查詢,並傳回物件。Executes SQL queries directly on the database and returns objects.

public:
generic <typename TResult>
 where TResult : gcnew() System::Collections::Generic::IEnumerable<TResult> ^ ExecuteQuery(System::String ^ query, ... cli::array <System::Object ^> ^ parameters);
public System.Collections.Generic.IEnumerable<TResult> ExecuteQuery<TResult> (string query, params object[] parameters) where TResult : new();
member this.ExecuteQuery : string * obj[] -> seq<'Result (requires 'Result : (new : unit -> 'Result))> (requires 'Result : (new : unit -> 'Result))
Public Function ExecuteQuery(Of TResult As New) (query As String, ParamArray parameters As Object()) As IEnumerable(Of TResult)

類型參數

TResult

所傳回集合中項目的型別。The type of the elements in the returned collection.

參數

query
String

要執行的 SQL 查詢。The SQL query to be executed.

parameters
Object[]

要傳遞至命令的參數陣列。The array of parameters to be passed to the command. 請注意下列情況:Note the following behavior: 如果陣列中的物件數目少於命令字串中所識別的最高數目,便會擲回例外狀況。If the number of objects in the array is less than the highest number identified in the command string, an exception is thrown.

如果陣列包含命令字串中未參考的物件,則不會擲回任何例外狀況。If the array contains objects that are not referenced in the command string, no exception is thrown.

如果參數為 null,則會轉換成 DBNull.ValueIf a parameter is null, it is converted to DBNull.Value.

傳回

IEnumerable<TResult>

查詢所傳回物件的集合。A collection of objects returned by the query.

備註

這個方法是一種傳遞機制,適用于不LINQ to SQLLINQ to SQL提供特定案例的情況。This method is a pass-through mechanism for cases where LINQ to SQLLINQ to SQL does not provide for a particular scenario.

查詢結果中的資料行與物件中的欄位和屬性相符的演算法,其運作方式如下:The algorithm for matching columns in the result of the query to fields and properties in the object works as follows:

  • 如果欄位或屬性已對應至特定資料行名稱,結果集 (Resultset) 必須包含該資料行名稱。If a field or property is mapped to a particular column name, that column name is expected in the resultset.

  • 如果欄位或屬性未對應,結果集必須包含與該欄位或屬性同名的資料行。If a field or property is not mapped, a column with the same name as the field or property is expected in the resultset.

  • 比較的執行方式是先尋找區分大小寫的相符。The comparison is performed by first looking for a case-sensitive match. 如果找不到符合的相符項,則會針對不區分大小寫的比對進行後續的搜尋。If such a match is not found, a subsequent search occurs for a case-insensitive match.

  • 當下列所有條件都成立時,查詢必須傳回物件的所有追蹤的欄位和屬性(除了要延後的載入以外):The query must return all the tracked fields and properties of the object (apart from those subject to deferred loading) when all the following are true:

    • 如果<T>是由明確追蹤的DataContext實體。If <T> is an entity explicitly tracked by the DataContext.

    • ObjectTrackingEnabled 為 true。ObjectTrackingEnabled is true.

    • 這個實體具有主索引鍵。The entity has a primary key.

    否則,會擲回例外狀況。Otherwise an exception is thrown.

  • 在所有其他情況下,查詢只會取得物件的追蹤欄位和屬性子集。In all other cases, the query can retrieve just a subset of the tracked fields and properties for the object.

下列C#程式碼片段顯示此方法的一種用法:The following C# snippet shows one use for this method:

var customers = db.ExecuteQuery<Customer>(@"SELECT CustomerID, CompanyName, ContactName, ContactTitle,   
   Address, City, Region, PostalCode, Country, Phone, Fax  
   FROM   dbo.Customers  
   WHERE  City = {0}", "London");  
  
foreach (Customer c in customers)  
   Console.WriteLine(c.ContactName);  

在 Visual BasicIn Visual Basic

Dim customers = db.ExecuteQuery(Of Customer)("SELECT CustomerID, _    CompanyName, ContactName, ContactTitle, _  
   Address, City, Region, PostalCode, Country, Phone, Fax _  
   FROM dbo.Customers _  
WHERE City = {0}", "London")  
  
For Each c As Customer In customers  
    Console.WriteLine(c.ContactName)  
Next  

適用於