Does anyone know how can I read the output variable from .net c#?
Example:
If I have the following stored proc which will return the output variables (@customer_id, @customer_name, @customer_address, @customer_age) instead of the select variable, how can I read the output variable with the following?
mySqlCommand.CommandText = "EXEC app_customers @name=" + sName.Text;
mySqlConnection.Open();
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
while (mySqlDataReader.Read())
{
}
When the result is a single value (or if you’re just interested in the first value in the first column), use the method
ExecuteScalar.It returns an object, simply cast it to the expected type.
Note: the way you’re invoking a procedure is not the normal way to do it. Set the command to reference the stored procedure, then add appropriate parameters to the
command.Parameterscollection. Invoking the procedure using"exec ..."is not a best practice and may even leave you vulnerable. If you need more info on executing such a call, start here.Edit:
If it is truly an output parameter you need to capture (I believe I misread your question), then the above paragraph is even more applicable. Consider this approach: