21

I know this is not a hell of an useful question but I can't help being bugged by it.

So,
Why said method (in *Command classes) is called
ExecuteNonQuery instead of ExecuteQuery?

Aren't those SQL statements we throw at DBs, queries?

Camilo Martin
  • 37,236
  • 20
  • 111
  • 154

3 Answers3

18

Semantically, a query is something you execute to return data. You're 'querying' the database to find all the X in the Y.

If you're not expecting back results, it's not so much a query as it is a statement or command.

Tejs
  • 40,736
  • 10
  • 68
  • 86
2

Not if they are INSERTs, DELETEs, CREATE TABLEs, etc.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • 1
    But a method that **can** execute a query (i.e., SELECT) should not be called ExecuteNonQuery. That's what bothers me. – Camilo Martin May 14 '10 at 12:30
  • 2
    It *can* execute a `SELECT`, but there's no point using `ExecuteNonQuery` with a `SELECT`, because it doesn't actually return a result. – Dean Harding May 14 '10 at 12:33
  • 2
    @Camilo the DB classes don't know if you need a response explicitly telling the DB object that you don't expect anything back makes room for optimizations. You as the dev might not want anything returned by a select statement (select * into ... for one) – Rune FS May 14 '10 at 12:34
  • Thanks Rune FS, it makes sense. – Camilo Martin May 14 '10 at 12:38
0

I would think of it as a query is asking the database for records back. Actions that alter the data/database would not be a query.

Jon
  • 5,956
  • 10
  • 39
  • 40