ALTER PROCEDURE tableuser
-- Add the parameters for the stored procedure here
@userName varchar(50)
AS
IF EXISTS(SELECT 'True' FROM tbl_user WHERE userName = @userName)
BEGIN
--This means it exists, return it to ASP and tell us
SELECT 'This record already exists!'
END
ELSE
BEGIN
--This means the record isn't in there already, let's go ahead and add it
SELECT 'Record Added'
INSERT into tbl_user(userName) VALUES(@username)
END
这是我在SQL Server Management Studio代码和下面的参数是C#代码:表预期已经存在
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("tableuser", conn);
conn.Open();
SqlParameter param = new SqlParameter();
cmd.Parameters.AddWithValue("@userName", uname.Text);
param.Value = uname.Text;
cmd.Parameters.Add(param);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
Response.Write("Username exists");
}
else
{
cmd.Parameters.AddWithValue("@userName", uname.Text);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open();
cmd.ExecuteNonQuery();
Response.Write("Successfully saved...!!!");
conn.Close();
}
}
}
的错误出现是:过程或函数' tableuser'需要参数'@userName',它没有提供。
你的C#代码非常混乱。它看起来像是检查行是否存在,如果不存在但是你的存储过程已经这样做了,就添加它。我想你在开始时也加入了多个参数,其中一个带有名字,另一个没有。 – acfrancis
我刚刚使用了数据库的一列用户名。你可以用C#的正确代码指定我吗? – user2920046