Why the "Non" in "ExecuteNonQuery"?
Asked Answered
B

3

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?

Beverie answered 14/5, 2010 at 12:26 Comment(0)
K
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.

Kylstra answered 14/5, 2010 at 12:28 Comment(3)
But it can be a SELECT. So why not just "myCommand.Execute()"?Beverie
@Camilo: Because there's ExecuteReader() that will return the results of your query, or even ExecuteScalar(), for single-valued results.Erythrocytometer
Thanks, I didn't knew of ExecuteScalar :)Beverie
P
2

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

Profession answered 14/5, 2010 at 12:28 Comment(4)
But a method that can execute a query (i.e., SELECT) should not be called ExecuteNonQuery. That's what bothers me.Beverie
It can execute a SELECT, but there's no point using ExecuteNonQuery with a SELECT, because it doesn't actually return a result.Excurvature
@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)Accessible
Thanks Rune FS, it makes sense.Beverie
L
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.

Labanna answered 14/5, 2010 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.