我正在编写一个登录脚本。但是当我想检查用户是否已经存在错误时:
Additional information: Procedure or function 'checkIfUserExcist' expects parameter '@username', which was not supplied.
会出现。
当我用一个参数执行存储过程时,它会成功。母 pig 我的代码有问题,但我找不到错误。
这是存储过程:
@username varchar(50)
as
begin
SELECT count(*)
FROM lars.userAcces
WHERE username = @username
end
这是我的 C# 代码:
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;
}
}
最佳答案
您需要告诉您的 SqlCommand
它使用存储过程 - 如下所示:
SqlCommand com = new SqlCommand("checkIfUserExcist", conn);
-- add this line here
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@username", username);
关于c# - SQL 服务器错误 "Procedure expects parameter",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30039640/