views:

195

answers:

3

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?

+1  A: 

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

Marcelo Cantos
But a method that **can** execute a query (i.e., SELECT) should not be called ExecuteNonQuery. That's what bothers me.
Camilo Martin
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
@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
Thanks Rune FS, it makes sense.
Camilo Martin
+8  A: 

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
But it can be a SELECT. So why not just "myCommand.Execute()"?
Camilo Martin
@Camilo: Because there's ExecuteReader() that will return the results of your query, or even ExecuteScalar(), for single-valued results.
Will Marcouiller
Thanks, I didn't knew of ExecuteScalar :)
Camilo Martin
A: 

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