Wednesday, May 10, 2023

What is the difference between ExecuteScalar, ExecuteReader, and ExecuteNonQuery?






Difference Between | ExecuteScalar, ExecuteReader, and ExecuteNonQuery?





ExecuteScalar():

(1) ExecuteScalar() works Non-Action Queries that contain aggregate functions.
(2) Return the first row and first column value of the query result.
(3) The return type is an object
(4)The return value is compulsory and should be assigned to a variable of the required type.



ExecuteReader():

(1) ExecuteReader() works with Action and Non-Action Queries (Select).
(2) Returns the collection of rows selected by the Query.
(3) The return type is DataReader
(4)The return value is compulsory and should be assigned to another object DataReader.



ExecuteNonQuery():

(1) ExecuteNonQuery() works with Action Queries only (Create, Alter, Drop, Insert, Update, Delete).
(2) Returns the count of rows affected by the Query.
(3) The return type is Int32
(4) For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.
(5) For all other types of statements, the return value is -1.






No comments:

Post a Comment