Difference between ExecuteNonQuery, ExecuteScalar and ExecuteReader

Ddd
Posted by Ddd under C# category on | Points: 40 | Views : 19981
Difference between ExecuteNonQuery, ExecuteScalar and ExecuteReader

1)ExecuteNonQuery

a) Used mainly for action queries(insert,delete,update,create,alter,drop).

It can also be used with select queries.

example: A stored procedure in SQL Server that can be used for autogeneration of ID's using its

output parameter can have the value of output parameter returned on a windows form using ExecuteNonQuery.

b)returns an int value indicating the number of affected rows


2)ExecuteScalar

a)Used in queries where we have to read a single value

example: 1)select count(*) from emp

2) select ename from emp where eno=100

b)returns an object


3)ExecuteReader

b)Used in queries where we have to read a complete row of the data

example: select * from emp where eno=100

b)It initializes a SqlDataReader/OleDbDataReader variable which can then

be used to read the data returned.

Comments or Responses

Login to post response