0
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',但未提供。

4

1 回答 1

0

这应该有效:

conn.Open();
var cmd = new SqlCommand("tableuser", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@userName", uname.Text);

var rdr = cmd.ExecuteReader();

rdr.Read();
Response.Write(rdr.GetString(0));
conn.Close();
于 2013-10-28T14:19:56.630 回答