What is the difference between ExecuteNonQuery, ExecuteReader, and ExecuteScalar?
Follow:
- ExecuteNonQuery: Executes SQL commands that do not return data (e.g., INSERT,
UPDATE, DELETE).
- Example: command.ExecuteNonQuery(); (used to insert a record).
- ExecuteReader: Executes SQL commands that return rows (e.g., SELECT queries).
It returns a DataReader.
- Example: command.ExecuteReader(); (used to select records).
- ExecuteScalar: Executes SQL commands and returns a single value (e.g., a single
cell of data).
- Example: command.ExecuteScalar(); (used for retrieving the count or
aggregate values).