且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server错误"过程需要参数"

更新时间:2022-10-23 17:47:14

您需要告诉你的的SqlCommand ,它使用存储过程 - 是这样的:

 的SqlCommand COM =新的SqlCommand(checkIfUserExcist,康恩); 
- 加入这一行这里
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue(@用户名的用户名);


I'm writing a login script. But when I want to check if user already exist the error:

Additional information: Procedure or function 'checkIfUserExcist' expects parameter '@username', which was not supplied.

will show up.

When I execute the stored procedure with one parameter it will succeed. Sow there is something wrong with my code, but I can't find the mistake.

This is the stored procedure:

@username varchar(50)
as
begin 
    SELECT count(*) 
    FROM lars.userAcces 
    WHERE username = @username
end

And this is my C# code:

public static bool checkIfUserExists(string username)
{
        int temp=0;

        SqlConnection conn = SqlConn.openSqlConnection();
        conn.Open();

        SqlCommand com = new SqlCommand("checkIfUserExists", conn);
        com.Parameters.AddWithValue("@username", username);

        temp = Convert.ToInt32(com.ExecuteScalar().ToString());
        conn.Close();

        if (temp == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
}

You need to tell your SqlCommand that it uses a stored procedure - like this:

SqlCommand com = new SqlCommand("checkIfUserExcist", conn);
-- add this line here
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@username", username);