Does ExecuteScalar() have any advantages over ExecuteReader()?
Does ExecuteScalar() have any advantages over ExecuteReader() ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
ExecuteScalaronly returns the first value from the first row of the dataset. Internal it is treated just likeExecuteReader(), aDataReaderis opened, the value is picked and theDataReadergets destroyed afterwards. I also always wondered about that behavior, but it has one advantage: It takes place within the Framework…and you can’t compete with the Framework in manners of speed.Edit By rwwilden:
Taking a look with Reflector inside
SqlCommand.ExecuteScalar()you can see these lines:Exactly what happens inside
ExecuteReader. Another advantage is thatExecuteScalarreturnsnullwhen no data is read. If you useExecuteReader, you’d have to check this yourself.