Difference between Executequery, Sqlquery and Executesqlcommand

Asked

Viewed 53 times

2

What is the difference between Executequery, Sqlquery and Executesqlcommand?

When I know which one to use?

  • 3

    Executequery and Sqlquery are used to execute standard SQL queries, a select and the like, since Executesqlcommand is mostly used to perform Update,Insert,Delete actions..

1 answer

6


  • Executesqlcommand: Executes a command, not for queries, or you will use to INSERT/UPDATE/DELETE.

  • Executequery: Execute a query with parameter, first receiving the Query and then the parameters as in the following example:

    db.ExecuteQuery<Customer>(@"SELECT CustomerID, CompanyName, ContactName, ContactTitle,Address, City,
    Region, PostalCode, Country, Phone, Fax FROM  dbo.Customers  WHERE  City = {0}", "London");  
    
  • Sqlquery: Executes a query (without passing parameters securely as in ExecuteQuery.

Note that ExecuteQuery is much like SqlQuery but it is recommended for safety to use it whenever you consult using parameters.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.