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?
ExecuteNonQuery: Use this operation to execute any arbitrary SQL statements in SQL Server if you do not want any result set to be returned. You can use this operation to create database objects or change data in a database by executing UPDATE, INSERT, or DELETE statements.
Apr, 2016 1. ExecuteNonQuery used for executing queries that does not return any data. It is used to execute the sql statements like update, insert, delete etc. ExecuteNonQuery executes the command and returns the number of rows affected. Jan, 2018 20.
You have to assign the connection for the command object. Your code is vulnerable to SQL injection. Also, you should use a using block with classes that implement the IDisposable interface, especially with the SqlConnection class in order to close the connection in case of an exception.
Solution 1. ExecuteScalar() only returns the value from the first column of the first row of your query. ExecuteReader() returns an object that can iterate over the entire result set. ExecuteNonQuery() does not return data at all: only the number of rows affected by an insert, update, or delete.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With