ExecuteScaler or ExecuterReader !?

  • Thread starter Thread starter SOS
  • Start date Start date
S

SOS

Hi guys ,
in many cases , i want to read a single value from the database ...
so , wich is faster , SqlCommand.ExecuteScaler or SqlCommand.ExecuterReader
?

Thanx
 
ExecuteScalar returns only the value you're after, so using it in the way you describe is its intended purpose. You'd have to do a very large number of calls to the database to see a difference in perfomance between ExecuteScalar and ExecuteReader (if the DataReader contains only one row with only one value, as you describe), so that's not what you should be basing your decision on. It's simply easier to code the ExecuteScalar call than the ExecuteReader call
 
SOS said:
Thanx , but why ?

ExecuteScalar returns the item in the first column of the results of the
query. ExecuteReader returns a DataReader that allows you to loop through
the results. ExecuteScalar is designed for reading a single returned value
(as in your case), whereas ExecuteReader is designed to work with multiple
rows with multiple columns.
 
Back
Top