【问题标题】:How to fix error number: 8114 'Procedure or function has too many arguments specified' in c# windows application如何修复错误号:8114 'Procedure or function has too many arguments specified' in c# windows application
【发布时间】:2016-05-12 12:24:15
【问题描述】:

我必须在 SQLServer 中创建过程,例如,

create procedure LeaveMasterProcdure(
@code nvarchar(20),
@type nvarchar(20),
@condition nvarchar(1000),
@leavedays nvarchar(50)
)
As
DECLARE @Max INT
        , @id varchar(50)

IF NOT EXISTS(SELECT id FROM LeaveMaster)
BEGIN
        SET @id = 'L001'                     
        INSERT INTO dbo.LeaveMaster(id,code,type,conditions,No_ofleaves)
        VALUES(@id,@code,@type,@condition,@leavedays)         
END
ELSE
BEGIN
        SELECT @Max = CONVERT(INT, SUBSTRING(CONVERT(NVARCHAR(10),id), 2,   10)) FROM LeaveMaster
        SET @id = 'L' + RIGHT('00' + CONVERT(NVARCHAR(10), @Max + 1), 5)
        INSERT INTO dbo.LeaveMaster(id,code,type,conditions,No_ofleaves)
        VALUES(@id,@code,@type,@condition,@leavedays)    
        END

另外,我通过过程插入值是在 SqlManagementStudio

中插入

但是当我尝试通过程序插入过程时,我得到 Procedure or function has too many arguments specified 异常。这段代码:

                SqlCommand AddLeave = con.CreateCommand();

                SqlParameter id = new SqlParameter("@id", SqlDbType.VarChar);
                SqlParameter code = new SqlParameter("@code", SqlDbType.VarChar);
                SqlParameter type = new SqlParameter("@type", SqlDbType.VarChar);
                SqlParameter conditions = new SqlParameter("@conditions", SqlDbType.VarChar);
                SqlParameter No_ofleaves = new SqlParameter("@No_ofleaves", SqlDbType.VarChar);

                AddLeave.Parameters.Add(id);
                AddLeave.Parameters.Add(code);
                AddLeave.Parameters.Add(type);
                AddLeave.Parameters.Add(conditions);
                AddLeave.Parameters.Add(No_ofleaves);

                SqlCommand ccc = new SqlCommand("select type from LeaveMaster", con);

                SqlDataReader r1 = ccc.ExecuteReader();
                while (r1.Read())
                {
                    s1 = r1["type"].ToString();
                }
                if (s1 == null)
                    id.Value = " ";
                else if (s1 != null)
                    id.Value = s1.ToString();
                r1.Close();


                code.Value = textBox_Leave_Code.Text;
                type.Value = textBox_Leave_Type.Text;
                conditions.Value = textBox_Conditions.Text;
                No_ofleaves.Value = textBox_total_leave.Text;

                AddLeave.Connection = con;

                AddLeave.CommandText = "LeaveMasterProcdure @id,@code,@type,@conditions,@No_ofleaves";


                try
                {
                    AddLeave.ExecuteNonQuery();
                    MessageBox.Show("added");
                }
                catch (Exception vv)
                {
                    MessageBox.Show(vv.ToString());
                }

我不知道如何解决它。请帮帮我...

【问题讨论】:

  • 您的程序中有 4 个参数,代码中有 5 个参数。他们应该被命名为相等。
  • 删除这一行SqlParameter id = new SqlParameter("@id", SqlDbType.VarChar);并将@No_ofleaves重命名为@leavedays
  • @tinka 不工作同样的异常
  • 请将各个地方No_ofleaves替换为leavedays
  • 查看您的过程创建命令 - 它没有 id 参数。尝试从 AddLeave.CommandText 行中删除“@id”。

标签: c# sql-server winforms stored-procedures


【解决方案1】:
  1. @id 参数不在过程中。
  2. 代码中的@conditions 与过程中的@condition 不匹配。
  3. @No_ofleaves 与过程中的 @leavedays 不匹配。

这里有一个简单的方法可以让这些更容易捕捉。将存储过程中的参数列表复制到您的 C# 代码中并将其注释掉。现在您将它们都放在一个地方,您不必来回切换。完成后删除 cmets。

我通常从我的 SQL 中复制和粘贴参数名称,因为它消除了拼写错误的可能性。

//@code nvarchar(20),
//@type nvarchar(20),
//@condition nvarchar(1000),
//@leavedays nvarchar(50)

SqlParameter id = new SqlParameter("@id", SqlDbType.VarChar);
SqlParameter code = new SqlParameter("@code", SqlDbType.VarChar);
SqlParameter type = new SqlParameter("@type", SqlDbType.VarChar);
SqlParameter conditions = new SqlParameter("@conditions", SqlDbType.VarChar);
SqlParameter No_ofleaves = new SqlParameter("@No_ofleaves", SqlDbType.VarChar);

【讨论】:

    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 2021-11-16
    • 1970-01-01
    • 2019-11-27
    相关资源
    最近更新 更多