Difference Between ExecuteNonQuery() And ExecuteScalar() Methods Using ADO.NET



Difference Between ExecuteNonQuery() And ExecuteScalar()



      ExecuteNonQuery()       ExecuteScalar()
1
It Returns the count of Rows Effected by the Query.
It Returns the First Row and First Column Value of the Query.
2
Int in Return Type.
Object in Return Type.
3
ExecuteNonQuery() works  Queries only. Create,Alter,Drop,Insert,Update,Delete.
ExecuteScalar() works Only Aggregate Functions
Max,Min,Count,Sum,Total.
4
Can be assigned to an integer variable.
Return value is optional.
Return value is Compulsory.
5
SqlCommand cmd=new SqlCommand();
cmd.ExecuteNonQuery();
SqlCommand cmd=new SqlCommand();
id = (Int16)cmd.ExecuteScalar();

return id;



0 comments:

Post a Comment