我正在尝试在C#中的SqlCommand
中执行存储过程。
这是C#中的代码:
string s = ConfigurationManager.ConnectionStrings["connection"].ToString();
SqlConnection conn = new SqlConnection(s);
conn.Open();
SqlCommand cmd = new SqlCommand("Signup1", conn);
cmd.CommandType = CommandType.StoredProcedure;
string password = TextBox2.Text;
cmd.Parameters.Add(new SqlParameter("@email", email));
SqlParameter pass = cmd.Parameters.Add("@password", SqlDbType.VarChar, 50);
pass.Value = password;
SqlParameter usertype = cmd.Parameters.Add("@usrtype", SqlDbType.VarChar, 50);
usertype.Value =usertype.Value;
cmd.ExecuteNonQuery();
conn.Close();
这是存储过程:
Create Proc Signup1
@email varchar(20),
@password Varchar(24),
@usrtype Varchar(30)
as
Insert into Members(email, password)
Values(@email, @password)
if @usrtype = 'Normal User'
begin
Insert into Normal_Users(email)
Values(@email)
end
else if @usrtype = 'Development Team'
begin
Insert into Development_Eeams(email)
Values(@email)
end
else if @usrtype = 'Verified Reviewer'
begin
Insert into Verified_reviewers(email)
Values(@email)
end
else
raiserror('Invalid type',16,1)
当我执行命令时,我收到此错误
过程或函数'Signup1'需要参数'@password',这是未提供的。
虽然我确实给了程序参数的值,但解决方案是什么?感谢
答案 0 :(得分:0)
string email = //where are you getting the email address
string password = TextBox2.Text;
string s = ConfigurationManager.ConnectionStrings["connection"].ToString();
using (SqlConnection connStr new SqlConnection(s);
using (SqlCommand cmd = new SqlCommand("Signup1", connStr))
{
c.Open();
command.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar) { Value = email });
command.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar) { Value = password });
command.Parameters.Add(new SqlParameter("@usrtype", SqlDbType.VarChar) { Value = userType }); //Where are you assigning userType
cmd.ExecuteNonQuery();
}
如果顶部示例是复杂的,那么您可以使用Parameters.AddWithValue
函数并让DB Server处理解析数据类型,例如
string email = //where are you getting the email address
string password = TextBox2.Text;
string s = ConfigurationManager.ConnectionStrings["connection"].ToString();
using (SqlConnection connStr new SqlConnection(s);
using (SqlCommand cmd = new SqlCommand("Signup1", connStr))
{
c.Open();
command.Parameters.AddWithValue("@email", email);
command.Parameters.AddWithValue"@password", password);
command.Parameters.AddWithValue("@usrtype", userType); //Where are you assigning userType
cmd.ExecuteNonQuery();
}