This article is describing the use of ExecuteScalar() method.
We can use ExecuteScalar() method, if we have to retrieve a single value from a database. It executes the query and returns the first column of the first row in the result set returned by the query. Additional rows and columns are simply discarded. It returns a maximum of 2033 characters. It is better to get the result of MAX,MIN,COUNT, etc using ExecuteScalar() method.
1. ExecuteScalar() requires the less code than using ExecuteReader().
2. ExecuteScalar() immediately returns the actual value. Hence, in case of retrieving single value from a database, it is fast and efficient.
The following figure shows the records inserted using cmd.ExecuteScalar() method.
Returns an object type
ExecuteScalar() method returns an object type.
So when i m trying to initialize the result of cmd.ExecuteScalar()
method in an integer variable, it is giving the below given error.
About the Author