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: 如果字段或属性映射到特定列名称,则结果集中应包含该列名称。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.

如果同时满足下列所有条件,则该查询应当返回(除延迟加载的对象外的)对象的所有跟踪的字段和属性: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: T 是由 DataContext 显式跟踪的实体。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:

  • 如果字段或属性映射到特定列名称,则结果集中应包含该列名称。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  

适用于