What if query for ExecuteScalar fetchs multiple rows?

September 8, 2006

This was something which I had worng impressions in my mind. I used to think if the query fetchs multiple tuples from DB and Command.ExecuteScalar() is used on that query, it might throw an exception. But thats not the case the .NET’s ExecuteScalar is optimised to return single column value of the first row of the resultset. Much more interesting fact about ExecuteScalar is that it boosts up application performance. If suppose we are designing a query to fetch MAX, SUM or COUNT values on some column in DB its always good to use ExecuteScalar because its optimised for such kind of operations.

ExecuteScalar returns the first column in the first row of resltset or NULL if resultset is empty.



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: