Click here to Skip to main content
       

ASP.NET

 
GeneralRe: Syntax error in store procedure PinmemberZafar Sultan26-Dec-12 4:31 
Questionid or guid.. which one? PinmemberJassim Rahma25-Dec-12 8:22 
AnswerRe: id or guid.. which one? PinmvpSandeep Mewara25-Dec-12 20:14 
AnswerRe: id or guid.. which one? [modified] PinmemberCommDev25-Dec-12 20:25 
Questionwhy should I care about ExecuteScalar PinmemberJassim Rahma25-Dec-12 6:57 
AnswerRe: why should I care about ExecuteScalar PinmvpSandeep Mewara25-Dec-12 20:30 
AnswerRe: why should I care about ExecuteScalar PinmemberCommDev25-Dec-12 20:33 
AnswerRe: why should I care about ExecuteScalar PinmemberCommDev25-Dec-12 20:40 
Hi,
 
ExecuteReader
 
Do not use: when database query is going to provide for sure exactly 1 record. It may be getting record by its id (which is PK in the database) - GetOrderById and such. In this case use ExecuteNonQuery with output parameters.
 
Use: when database query is going to provide a set of records. It may be search or report.
 
ExecuteNonQuery
 
Use: when we are talking about a single database record - in Update, Insert, Delete and Get by Id. In all these cases we can use input/output/input-output parameters. Please note that from the application architecture point of view it is also good practices when your Insert and Update stored procedure returns changed record exactly like Get By Id method does.
 
ExecuteScalar
 
Do not use: when database query returns a single value and this value can be defined as parameter in T-SQL. ExecuteNonQuery with output parameter(s) is always preferred in this case since it is more flexible, tomorrow there will be 2 values therefore having ExecuteNonQuery we do not need to change method signatures.
 
Use: when database query returns a single value and this value cannot be defined as output parameter, because of T-SQL type limitation for variables. For example type image cannot be output parameter in MSSQL.
 
The most common example for ExecuteScalar is fetching a single image stored in the database and converting it to array of bytes. If you google it - most examples will demonstrate using of ExecuteReader to accomplish image handler, however ExecuteScalar will be more scalable and faster.
 
Conclusion
 
Always use ExecuteNonQuery except: when you have a set of records - use ExecuteReader and when you have a single output value that cannot be defined as a parameter - use ExecuteScalar. Hope this helped to clarify something. Enjoy Smile | :)
Comm100 - Leading Live Chat Software Provider

GeneralRe: why should I care about ExecuteScalar PinmemberJassim Rahma28-Dec-12 10:40 
GeneralRe: why should I care about ExecuteScalar PinmemberNaveen Katta26-Dec-12 1:10 
Questiona control that should contain html elements [modified] PinmemberAli Al Omairi(Abu AlHassan)24-Dec-12 0:01 
QuestionRe: a control that should contain html elements Pinmemberjkirkerx24-Dec-12 14:09 
AnswerRe: a control that should contain html elements PinmemberAli Al Omairi(Abu AlHassan)25-Dec-12 20:51 
Questioncheckedchange event of gridview in javascript PinmemberRavi_kant23-Dec-12 20:29 
AnswerRe: checkedchange event of gridview in javascript PinmemberSampath Kumar Sathiya24-Dec-12 1:20 
GeneralRe: checkedchange event of gridview in javascript PinmemberRavi_kant24-Dec-12 8:40 
Questionupdated data from gridview PinmemberMember 970720723-Dec-12 1:30 
AnswerRe: updated data from gridview PinmemberAli Al Omairi(Abu AlHassan)24-Dec-12 5:31 
AnswerRe: updated data from gridview PinmvpSandeep Mewara25-Dec-12 20:35 
Question.net question PinmemberTarani Dutt Joshi22-Dec-12 0:53 
AnswerRe: .net question PinmvpRichard MacCutchan22-Dec-12 1:18 
AnswerRe: .net question PinmemberMaulikDusara24-Dec-12 22:38 
AnswerRe: .net question PinmvpSandeep Mewara25-Dec-12 20:33 
QuestionWebRequest Pinmembercdpsource21-Dec-12 15:16 
Questionwebpage has resulted in too many redirects PinmemberJassim Rahma21-Dec-12 10:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 26 Dec 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid