3 July 2012

Difference between Excute NonQurey(),ExcuteReader() and ExcuteScalar()

ExecuteNonQuery():
1.will work with Action Queries only (Create,Alter,Drop,Insert,Update,Delete).
2.Retruns the count of rows effected by the Query.
3.Return type is int
4.Return value is optional and can be assigned to an integer variable.

ExecuterReader():
1.will work with Action and Non-Action Queries (Select)
2.Returns the collection of rows selected by the Query.
3.Return type is DataReader.
4.Return value is compulsory and should be assigned to an another object DataReader.

ExecuteScalar():
1.will work with Non-Action Queries that contain aggregate functions.
2.Return the first row and first column value of the query result.
3.Return type is object.
4.Return value is compulsory and should be assingned to a variable of required type.