When you use ExecuteNonQuery you do not get any rows returned. When you use ExecuteReader you get the returned rows. Your stored procedure is returning rows, therefore you will need to use ExecuteReader otherwise you won't ne able to read the rows.
The stored procedure could create a database, drop tables, send email, create powerpoint presentations, delete files from network shares - it doesn't matter. What counts is whether you wish to access the relation that is returned.
ToughMan
Participant
1490 Points
635 Posts
What method should I use?
Nov 25, 2012 08:25 AM|LINK
Now all of you must know that for a SqlCommand——
1)If it includes a Select command,u should use ExecuteReader.
2)If it includes Insert,Update or Delete,u should use ExecuteNonQuery().
Now my questions are:
1)If I have a statement like this:
I mean all the CRUD SQL statements are included together,So which method should I use?
2)If I have the above four statements in a SP,So which method should I use?
ignatandrei
All-Star
137698 Points
22155 Posts
Moderator
MVP
Re: What method should I use?
Nov 25, 2012 08:35 AM|LINK
two methods: one for insert, delete , update and one for Select
2. execute datareader
oned_gk
All-Star
36078 Points
7363 Posts
Re: What method should I use?
Nov 25, 2012 10:00 AM|LINK
Suwandi - Non Graduate Programmer
ToughMan
Participant
1490 Points
635 Posts
Re: What method should I use?
Nov 25, 2012 10:34 AM|LINK
Hi ignatandrei,
My 1st mean that I'll use ";" to seperate the CRUD methods. So which method should I use?
ToughMan
Participant
1490 Points
635 Posts
Re: What method should I use?
Nov 25, 2012 10:35 AM|LINK
Sorry but if I insist using SqlComand?
ignatandrei
All-Star
137698 Points
22155 Posts
Moderator
MVP
Re: What method should I use?
Nov 25, 2012 01:45 PM|LINK
Use 1 SqlCommand for CUD and executenonquery
Use 1 SqlCommand for R and executeReader
Paul Linton
Star
13581 Points
2571 Posts
Re: What method should I use?
Nov 25, 2012 08:43 PM|LINK
When you use ExecuteNonQuery you do not get any rows returned. When you use ExecuteReader you get the returned rows. Your stored procedure is returning rows, therefore you will need to use ExecuteReader otherwise you won't ne able to read the rows.
The stored procedure could create a database, drop tables, send email, create powerpoint presentations, delete files from network shares - it doesn't matter. What counts is whether you wish to access the relation that is returned.
ToughMan
Participant
1490 Points
635 Posts
Re: What method should I use?
Nov 26, 2012 12:33 AM|LINK
So I'd just confirm that you mean u just wanna mean if I nest strings directly into SqlCommand as its parameter, I can use ExeuteNonQuery()?
Paul Linton
Star
13581 Points
2571 Posts
Re: What method should I use?
Nov 26, 2012 12:36 AM|LINK
You use ExecuteNonQuery if you don't want any rows returned.
ignatandrei
All-Star
137698 Points
22155 Posts
Moderator
MVP
Re: What method should I use?
Nov 26, 2012 03:03 AM|LINK
and non return anything/