c# - SQL 服务器错误 "Procedure expects parameter"

标签 c# sql sql-server

我正在编写一个登录脚本。但是当我想检查用户是否已经存在错误时:

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/

相关文章:

c# - 在 Windows 窗体中显示 SolidWorks 工程图

sql-server - SSIS - 多种配置

c# - SQL Server 查询不在 asp.net 中返回任何结果

C# sql 创建一个连接并为每个查询打开和关闭

sql - Postgres 选择一个 concat 字段并在 Rails 中喜欢它

c# - 安全软件许可证使用审计日志

c# - 工厂反射(reflection)是一种好的做法吗?

c# - 如果您不控制所涉及的类型,您将如何将 "switch on Type"重构为多态?

PHP sqlsrv查询到数据库

Mysql 选择虚拟一列中的两列