// 打开数据库连接
conn.Open(); // 创建用于执行数据库操作的命令对象, GetUNameById为存储过程名称
SqlCommand cmd =new SqlCommand("GetUNameById", conn); // 设置执行命令的方式为存储过程
cmd.CommandType = CommandType.StoredProcedure; // 向命令对象添加存储过程所需要的参数
cmd.Parameters.Add("@uid", SqlDbType.Int); // 设置要传入到存储过程的参数值
cmd.Parameters["@uid"].Value =2; // 添加存储过程中的输出参数,如果是字符型的必须定义长度
cmd.Parameters.Add("@name", SqlDbType.VarChar, 50); // 设置参数为output输出参数
cmd.Parameters["@name"].Direction = ParameterDirection.Output; // 执行存储过程
cmd.ExecuteReader(); // 获取执行存储过程后的输出参数
string name = cmd.Parameters["@name"].Value.ToString(); Response.Write(name);
转载于:https://www.cnblogs.com/guozhe/archive/2012/07/24/2606313.html