How to control what gets returned by command.ExecuteNonQuery()?

  • Thread starter Thread starter keithb
  • Start date Start date
K

keithb

What can I put in a stored procedure to control what gets returned by
command.ExecuteNonQuery()? I already tried this:

param = comm.CreateParameter();

param.ParameterName = "@Success";

param.Value = Convert.ToInt32(1);

param.DbType = DbType.Int32;

param.Direction = ParameterDirection.ReturnValue;

comm.Parameters.Add(param);

With a corresponding parameter in the stored procedure, but the value of
@Success still does not get returned by command.ExecuteNonQuery();

Thanks,

Keith
 
ExecuteNonQuery returns the number of rows affected. To get your param
value, do

comm.ExecuteNonQuery();
int returnValue = Convert.ToInt32(comm.Parameters["@Success"]);

Note that this'll die if success is NULL, there are various patterns
for fixing that depending on your framework version and personal
preferences.
 
ExecuteNonQuery returns the number of rows affected. To get your param
value, do

comm.ExecuteNonQuery();
int returnValue = Convert.ToInt32(comm.Parameters["@Success"].Value);

Note that this'll die if success is NULL, there are various patterns
for fixing that depending on your framework version and personal
preferences.
 
lets look a simple proc

create procedure foo @value int out
as
set @value=1
return 2

then in code

comm.CommandText = "foo";
paramValue = comm.CreateParameter();
paramValue.ParameterName = "@Value";
paramValue.DbType = DbType.Int32;
paramValue.Direction = ParameterDirection.Output;
comm.Parameters.Add(paramValue);

paramReturn = comm.CreateParameter();
paramReturn.ParameterName = "@Success";
paramReturn.DbType = DbType.Int32;
paramReturn.Direction = ParameterDirection.ReturnValue;
comm.Parameters.Add(paramReturn);

comm.ExecuteNonQuery();

now

paramValue.Value will equal 1
paramReturn.Value will equal 2


-- bruce (sqlwork.com)
 

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

Back
Top