Tuesday 2 June 2015

Diffrence between ExecuteScalar() and ExecuteNonQuery()

ExecuteScalar() Method:

ExecuteScalar() method is used to retrieve a single value from database. It executes the defined query and returns the value in the first column of the first row in the selected result set and ignores all other columns and rows in the result set. It is use to get aggregate value from database, for example count or total of rows. So it works with non action queries that use aggregate functions. ExecuteScalar() method is a faster way when we compare it to other ways to retrieve single value from database. It returns a value as object and we have to cast it to appropriate type.


ExecuteNonQuery() Method:

ExecuteNonQuery() method is used to manipulate data in database and is used for statements without results such as CREATE, INSERT, UPDATE and DELETE commands. It does not return any data but it returns number of rows affected. If NO COUNT property is ON then it will not return number of rows affected. It will not give access to result set generated by the statement. The return value of number of rows affected is of type integer and you can get it in an integer variable. It will tell you how many rows have been affected in result of your statement. ExecuteNonQuery() method is a flexible method and we can use it input and output parameters.

No comments:

Post a Comment