Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ObjectContext.ExecuteStoreQuery(TEntity) Method (String, String, MergeOption, Object[])

.NET Framework 4.6 and 4.5

Executes a query directly against the data source and returns a sequence of typed results. Specify the entity set and the merge option so that query results can be tracked as entities.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

member ExecuteStoreQuery : 
        commandText:string * 
        entitySetName:string * 
        mergeOption:MergeOption * 
        parameters:Object[] -> ObjectResult<'TEntity> 

Type Parameters

TEntity

Parameters

commandText
Type: System.String

The command to execute, in the native language of the data source.

entitySetName
Type: System.String

The entity set of the TResult type. If an entity set name is not provided, the results are not going to be tracked.

mergeOption
Type: System.Data.Objects.MergeOption

The MergeOption to use when executing the query. The default is AppendOnly.

parameters
Type: System.Object[]

An array of parameters to pass to the command.

Return Value

Type: System.Data.Objects.ObjectResult(TEntity)
An enumeration of objects of type TResult.

The ExecuteStoreQuery method uses the existing connection to execute an arbitrary command directly against the data source. The store command is executed in the context of the current transaction, if such a transaction exists.

Calling the ExecuteStoreQuery method is equivalent to calling the ExecuteReader method of the DbCommand class, only ExecuteStoreQuery returns entities and the ExecuteReader returns property values in the DbDataReader.

Specify the entity set name, if you want for the results to be tracked as entities.

Call Translate method to translate a DbDataReader into entity objects when the reader contains data rows that map to the specified entity type.

Using parameterized commands helps guard against SQL injection attacks, in which an attacker "injects" a command into a SQL statement that compromises security on the server. Parameterized commands guard against a SQL injection attack by guaranteeing that values received from an external source are passed as values only, and not part of the SQL statement. As a result, SQL commands inserted into a value are not executed at the data source. Rather, they are evaluated only as a parameter value. In addition to the security benefits, parameterized commands provide a convenient method for organizing values passed with a SQL statement or to a stored procedure.

The parameters value can be an array of DbParameter objects or an array of parameter values. If only values are supplied, an array of DbParameter objects are created based on the order of the values in the array.

For more information, see:

Executing Store Commands and

How to: Directly Execute Arbitrary Command Against the Store

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4
Show:
© 2015 Microsoft