How to use ExecuteScalar() method
Posted by Zinnia Sarkar
on 1/31/2011 for Beginner level | Points: 250
| Views : 32990
If you found plagiarised (copied)
or inappropriate content,
please let us know
the original source along with your correct email id (to communicate) for further action.
Rating: 3 out of 5
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.
Found interesting? Add this to: