Home >Database >Mysql Tutorial >How to Properly Retrieve Stored Procedure Return Values in C#?

How to Properly Retrieve Stored Procedure Return Values in C#?

Barbara Streisand
Barbara StreisandOriginal
2025-01-05 20:19:45225browse

How to Properly Retrieve Stored Procedure Return Values in C#?

Retrieving Stored Procedure Return Values in C#

Consider the following stored procedure:

ALTER PROCEDURE [dbo].[Validate]
@a varchar(50),
@b varchar(50) output
AS
SET @Password = 
(SELECT Password
FROM dbo.tblUser
WHERE Login = @a)
RETURN @b
GO

This procedure compiles successfully. To execute this query and obtain the return value, you might write code similar to the following:

using System.Data.SqlClient;
using System.Configuration;

...

SqlConnection SqlConn = new SqlConnection(
    ConfigurationManager.ConnectionStrings["MyLocalSQLServer"].ConnectionString.ToString());
System.Data.SqlClient.SqlCommand sqlcomm = new System.Data.SqlClient.SqlCommand("Validate", SqlConn);

string returnValue = string.Empty;
SqlConn.Open();

sqlcomm.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@a", SqlDbType.VarChar);
param.Direction = ParameterDirection.Input;
param.Value = Username;
sqlcomm.Parameters.Add(param);

SqlParameter retval = sqlcomm.Parameters.Add("@b", SqlDbType.VarChar);
retval.Direction = ParameterDirection.ReturnValue;

sqlcomm.ExecuteNonQuery(); // MISSING
string retunvalue = (string)sqlcomm.Parameters["@b"].Value;

Note that the exception handling has been omitted for brevity. Upon executing the last line of the code, 'null' is returned. To resolve this issue, you have forgotten to execute the query. Add sqlcomm.ExecuteNonQuery(); before retrieving the return value, as shown below:

using System.Data.SqlClient;
using System.Configuration;

...

SqlConnection SqlConn = new SqlConnection(
    ConfigurationManager.ConnectionStrings["MyLocalSQLServer"].ConnectionString.ToString());
System.Data.SqlClient.SqlCommand sqlcomm = new System.Data.SqlClient.SqlCommand("Validate", SqlConn);

string returnValue = string.Empty;
SqlConn.Open();

sqlcomm.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@a", SqlDbType.VarChar);
param.Direction = ParameterDirection.Input;
param.Value = Username;
sqlcomm.Parameters.Add(param);

SqlParameter retval = sqlcomm.Parameters.Add("@b", SqlDbType.VarChar);
retval.Direction = ParameterDirection.ReturnValue;

sqlcomm.ExecuteNonQuery();  // Include this line to execute the query

string retunvalue = (string)sqlcomm.Parameters["@b"].Value;

By incorporating this change, you should be able to retrieve the return value from the stored procedure successfully.

The above is the detailed content of How to Properly Retrieve Stored Procedure Return Values in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn