c# - 过程或函数需要未提供的参数

标签 c# sql asp.net sql-server stored-procedures

Procedure or function 'login' expects parameter '@Abc', which was not supplied

4 小时搜索和尝试但没有用我已经提供了这个参数(复制/粘贴)并且给过程的参数数量与过程相同并且顺序相同。

@Abc 是一个输出参数。

存储过程定义:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[login]
(
    @Abc int output,
    @firstname varchar(255) output,
    @lastname varchar(255) output,
    @Email varchar(255),
    @pass varchar(255)
)
As 
begin 
if not exists (select Email from user_1 where  email=@email)
 select @Abc = 0
 else begin
if not exists (
    select Email from user_1 where email =@Email and password = @pass
    )
    select @Abc = 1
else
select @Abc = 2,@firstname = u.first_name ,@lastname=u.last_name from user_1 u where u.email =   @email 
end
end 

调用存储过程的代码:

 myCon.Open();

 TextBox username = UserName;
 TextBox password = Password;

 SqlCommand myCommand = new SqlCommand("login", myCon);

 SqlParameter count= myCommand.Parameters.Add("@Abc", SqlDbType.Int);
 count.Direction = ParameterDirection.Output;

 SqlParameter fnp = myCommand.Parameters.Add("@firstname", SqlDbType.VarChar,255);
 fnp.Direction = ParameterDirection.Output;

 SqlParameter lnp = myCommand.Parameters.Add("@lastname", SqlDbType.VarChar, 255);
 lnp.Direction = ParameterDirection.Output;

 myCommand.Parameters.AddWithValue("@Email",username.Text);
 myCommand.Parameters.AddWithValue("@pass", password.Text);

 myCommand.ExecuteNonQuery();
 myCon.Close();

最佳答案

你遗漏了:

myCommand.CommandType = CommandType.StoredProcedure;

因此发送到数据库的命令是错误的 sp_executeSQL 调用而不是所需的 exec login

仅供引用,还有一个更短的语法:

myCommand.Parameters.Add("@Abc", SqlDbType.Int).Direction = ParameterDirection.Output;

关于c# - 过程或函数需要未提供的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27188849/

相关文章:

c# - 从 SQL Exception 中获取重复键值

c# - 添加鼠标点击事件到面板

c# - 双? = 双? + 双?

SQL 服务器 : How can I check to see if a field has a "NULL" or "NOT NULL" constraint in place?

c# - Udpclient 变量不断重置为 Null ASP.NET

c# - 为 .NET 数据访问层使用 MySql 存储过程

mysql - 从数据库中选择大量值的最有效方法

asp.net - IIS Url Rewrite 的 preCondition 属性上的模式 ="."是什么意思?

asp.net - 您如何使用 ActionFilters 中断/拦截 MVC 操作?

sql - 无法从 BLOB 存储打开备份设备 - 访问被拒绝