SQL Server错误"过程需要参数" [英] SQL Server error "Procedure expects parameter"
问题描述
我正在写一个登录脚本。但是,当我要检查,如果用户已经存在的错误:
更多信息:过程或函数'checkIfUserExcist'需要参数'@用户名 ,这是没有提供。
块引用>
会显示出来。
当我与一个参数执行存储过程就会成功。母猪有什么错我的代码,但我不能发现其中的错误。
这是存储过程:
@username VARCHAR(50 )
为
开始
SELECT COUNT(*)
FROM lars.userAcces
其中username = @用户名
端
这是我的C#代码:
公共静态布尔checkIfUserExists(用户名字符串)
{
INT温度= 0;
的SqlConnection康恩= SqlConn.openSqlConnection();
conn.Open();
的SqlCommand COM =新的SqlCommand(checkIfUserExists,康恩);
com.Parameters.AddWithValue(@用户名的用户名);
TEMP = Convert.ToInt32(com.ExecuteScalar()的ToString());
conn.Close();
如果(临时== 1)
{
返回真;
}
,否则
{
返回FALSE;
}
}
解决方案您需要告诉你的
的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);
这篇关于SQL Server错误"过程需要参数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!