stored procedure and return value

J

Jeff

Hey

..NET 2.0

I have a stored procedure which calculates a value and return that value.

I'm wondering if I should use ExecuteNonQuery, ExecuteScalar or
ExecuteReader on this stored procedure. I want that return value from the
stored procedure. AFAIK I can use ExecuteReader, but maybe the other options
are much better?

any suggestions?

Jeff
 
M

Marc Gravell

Any are fine; the return value is actually used by adding a parameter
to the command with the direction of ReturnValue. After you have
executed the proc, you can obtain the return value via .Value. Note,
however, that if you use ExecuteReader etc (i.e. rows are involved)
then you must walk all the data before you can get out/return values,
since they are positioned at the end of the data-stream.

Marc
 
M

Marc Gravell

I forgot to say, but in general ExecuteNonQuery is a better option if
you don't expect data, simply because it can perform some
optimisations internally. This might have been your real question...

Marc
 
J

Jeff

thanks for replying to my post :)

What's confuse me with ExecuteNonQuery is that the documentation says that
ExecuteNonQuery returns the number of rows affected by the stored procdeure.
I have a fear that ExecuteNonQuery will return the number of rows instead of
my return value..

any thoughts about this?
 
G

Guest

For me ExecuteScalar will be better, because u can get the return value from
this method rather being depend on output parameter
 
N

Nicholas Paldino [.NET/C# MVP]

Jeff,

Yes, the call to ExecuteNonQuery will return the number of rows
affected, but the return value will be in the parameters collection. If you
want the return value from the stored procedure, you have to access the
Parameters collection on the command, and then access the parameter which
has the return value.
 
H

Hans Kesting

Vivek expressed precisely :
For me ExecuteScalar will be better, because u can get the return value from
this method rather being depend on output parameter

Note: ExecuteScalar will not return the value that is supplied with the
"return" statement in the stored procedure, it will return the first
value of the first row of data. So to use this you need to "select" the
value, not "return" it.

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top