InsertUser'期望参数'@Username',该参数未提供

本文关键字:参数 InsertUser @Username 期望 | 更新日期: 2023-09-27 18:19:19

我收到错误信息

过程或函数'InsertUser'期望参数'@Username',该参数未提供

请帮我解决这个问题。

存储过程

ALTER PROCEDURE [dbo].[InsertUser] (
    @Username varchar(50), 
    @Password varchar(50)
)
AS 
    INSERT INTO logform(Username, Password) 
    VALUES(@Username, @Password)

我的连接类

public class connection
{
    SqlConnection conn;
    SqlCommand cmd;
    public connection()
    {
        conn = new SqlConnection(@"server=SEMIL-PC'SQLEXPRESS;database=semil;Integrated security=true");
        cmd = null;
    }
    public void nonquery(string qry)
    {
        conn.Open();
        cmd = new SqlCommand(qry, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}
我班上

public class master
{
    connection conn = new connection();
    SqlCommand cmd = null;
    public void insert(string username, string password)
    {
        string qry = "InsertUser";
        SqlCommand cmd = new SqlCommand("qry");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value =username;
        cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value =password;
        conn.nonquery(qry);
    }
}

我的按钮点击功能

protected void Button1_Click(object sender, EventArgs e)
{
       mas.insert(txtuser.Text, txtpass.Text);
       ClientScript.RegisterStartupScript(GetType(), "alert", "alert('logged in');", true);
}

请帮助我.......

InsertUser'期望参数'@Username',该参数未提供

您在connection类的executenonquery方法中看到这一行了吗?

cmd = new SqlCommand(qry, conn);

这将创建一个没有定义任何参数的新命令。当然失败了。

修复代码的最快方法是更改executenonquery方法以接收已构建的命令或为其创建重载

public void nonquery(SqlCommand cmd)
{
        conn.Open();
        cmd.Connection = conn;
        cmd.ExecuteNonQuery();
        conn.Close();
}

并传递您在主类

的插入方法中构建的命令
public class master
{
    connection conn = new connection();
    SqlCommand cmd = null;
    public void insert(string username, string password)
    {
        SqlCommand cmd = new SqlCommand("InsertUser");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value =username;
        cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value =password;
        conn.nonquery(cmd);
    }
}

问题似乎是您的master/connection类之间的脱节。在master中,您设置命令,配置参数,然后将qry传递给connection,这将创建另一个命令(不带参数)。

看起来你试图在这里做的是一个重用SqlConnection实例,因此你应该将cmd传递给connection,例如

public void insert(string username, string password)
{
    SqlCommand cmd = new SqlCommand("InsertUser");
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value =username;
    cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value =password;
    conn.nonquery(cmd);
}

public void nonquery(SqlCommand cmd)
{
    conn.Open();
    cmd.Connection = conn;
    cmd.ExecuteNonQuery();
    conn.Close();
}

或者你可以考虑使nonquery更通用,并允许传入参数,例如

public void nonquery(string sql, Dictionary<string, object> parameters, CommandType type = CommandType.Text)
{
    using (var cmd = new SqlCommand(sql, conn))
    {
        cmd.Parameters.AddRange(parameters.Select(x => new SqlParameter(x.Key, x.Value)));
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}
...
var parameters = new Dictionary<string, object>(2)
{
    { "@Username", username },
    { "@Password", password }
};
nonquery("InsertUser", parameters, CommandType.StoredProcedure);

这将使它可以被其他命令重用

var parameters = new Dictionary<string, object>(1)
{
    { "@Username", username }
};
nonquery("select * from user where Username = @Username", parameters);