首页 > 代码库 > c#调用带输出参数的存储过程
c#调用带输出参数的存储过程
CREATE PROCEDURE ProGetPWD
@username varchar(20),
@password varchar(20) OUTPUT
AS
BEGIN
SELECT @password = password
FROM Users
WHERE username = @username
END
string sql = "Data Source=.;Initial Catalog=Test;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(sql))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
//设置要调用的存储过程的名称
cmd.CommandText = "GetuserPwd";
//指定SqlCommand对象传给数据库的是存储过程的名称而不是sql语句
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter userName = cmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.VarChar, 20));
//指明"@userName"是输入参数
userName.Direction = ParameterDirection.Input;
//为@userName参数赋值
userName.Value = http://www.mamicode.com/txtUserName.Text;
SqlParameter userPwd = cmd.Parameters.Add(new SqlParameter("@userPwd", SqlDbType.VarChar, 20));
//指明"@userPwd"是输出参数
userPwd.Direction = ParameterDirection.Output;
//执行
cmd.ExecuteNonQuery();
//得到输出参数的值,把赋值给name,注意,这里得到的是object类型的,要进行相应的类型轮换
string passwrod = Convert.ToString(cmd.Parameters["@userPwd"].Value);
MessageBox.Show(passwrod);
}
c#调用带输出参数的存储过程