SoFunction
Updated on 2025-03-06

C# Method for obtaining the value of the stored procedure and the output parameter value

1. Get the return return value

Copy the codeThe code is as follows:

//Stored Process
//Create PROCEDURE MYSQL
//     @a int,
//     @b int
//AS
//     return @a + @b
//GO
SqlConnection conn = new SqlConnection(["LocalSqlServer"].ToString());
();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
= ;
(new SqlParameter("@a", ));
["@a"].Value = 10;
(new SqlParameter("@b", ));
["@b"].Value = 20;
(new SqlParameter("@return", ));
["@return"].Direction = ;
();
(["@return"].());

2. Get Output output parameter value

Copy the codeThe code is as follows:

//Stored Process
//Create PROCEDURE MYSQL
//     @a int,
//     @b int,
//     @c int output
//AS
//     Set @c = @a + @b
//GO
SqlConnection conn = new SqlConnection(["LocalSqlServer"].ToString());
();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
= ;
(new SqlParameter("@a", ));
["@a"].Value = 20;
(new SqlParameter("@b", ));
["@b"].Value = 20;
(new SqlParameter("@c", ));
["@c"].Direction = ;
();
(["@c"].());

C# receives the stored procedure return value:

Copy the codeThe code is as follows:

     public static int User_Add(User us)
     {
         int iRet;
         SqlConnection conn = new SqlConnection(Conn_Str);
         SqlCommand cmd = new SqlCommand("User_Add", conn);
         = ;
         ("@UName", );
         ("@UPass", );
         ("@PassQuestion", );
         ("@PassKey", );
         ("@Email", );
         ("@RName", );
         ("@Area", );
         ("@Address", );
         ("@ZipCodes", );
         ("@Phone", );
         ("@QQ", );
         ("@RETURN_VALUE", "").Direction = ;      
         try
         {
             ();
             ();
             iRet = (int)["@RETURN_VALUE"].Value;
         }
         catch (SqlException ex)
         {
             throw ex;
         }
         finally
         {
             ();
         }
         return iRet;
     }

C# receives stored procedure output parameters:

Copy the codeThe code is as follows:

    public static decimal Cart_UserAmount(int UID)
    {
        decimal iRet;
        SqlConnection conn = new SqlConnection(Conn_Str);
        SqlCommand cmd = new SqlCommand("Cart_UserAmount", conn);
        = ;
        ("@UID", UID);
        ("@Amount", ).Direction=;
        try
        {
            ();
            ();
            iRet = (decimal)["@Amount"].Value;
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            ();
        }
        return iRet;
    }