C#获取存储过程返回值和输出参数值
1.获取Return返回值 程序代码
//存储过程 //Create PROCEDURE MYSQL // @a int, // @b int //AS // return @a + @b //GO SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString()); conn.Open(); SqlCommand MyCommand = new SqlCommand("MYSQL", conn); MyCommand.CommandType = CommandType.StoredProcedure; MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int)); MyCommand.Parameters["@a"].Value = 10; MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int)); MyCommand.Parameters["@b"].Value = 20; MyCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int)); MyCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue; MyCommand.ExecuteNonQuery(); Response.Write(MyCommand.Parameters["@return"].Value.ToString());
程序代码
//存储过程 //Create PROCEDURE MYSQL // @a int, // @b int, // @c int output //AS // Set @c = @a + @b //GO SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString()); conn.Open(); SqlCommand MyCommand = new SqlCommand("MYSQL", conn); MyCommand.CommandType = CommandType.StoredProcedure; MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int)); MyCommand.Parameters["@a"].Value = 20; MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int)); MyCommand.Parameters["@b"].Value = 20; MyCommand.Parameters.Add(new SqlParameter("@c", SqlDbType.Int)); MyCommand.Parameters["@c"].Direction = ParameterDirection.Output; MyCommand.ExecuteNonQuery(); Response.Write(MyCommand.Parameters["@c"].Value.ToString());
---------------------------------------------------------------------------------------------------------------
以下代码转自网络:
C#接收存储过程返回值: public static int User_Add(User us) C#接收存储过程输出参数: public static decimal Cart_UserAmount(int UID) |