简体   繁体   English

过程或函数指定了太多参数

[英]Procedure or function has too many arguments specified

There are so many questions on SO on this exception. 关于这个例外,关于SO有很多问题。 But none of them are useful to me. 但是它们都不对我有用。

Here is my Stored Procedure : 这是我的存储过程:

CREATE PROCEDURE HolidayStandardMappingInsert 
    @HolidayID bigint,
    @StandatdID smallint
AS
BEGIN
    INSERT INTO HolidayStandardMapping VALUES(@HolidayID,@StandatdID)
END
GO

And here is my Code: 这是我的代码:

int StdId = 0;
                SqlCommand cmdS = new SqlCommand("HolidayStandardMappingInsert", conn);
                cmdS.CommandType = CommandType.StoredProcedure;


                for (int i = 0; i < cblStandard.Items.Count; i++)
                {
                    if (cblStandard.Items[i].Selected == true)
                    {
                        if (StdId == 0)
                            StdId = Convert.ToInt32(cblStandard.Items[i].Value);
                        else
                            StdId = Convert.ToInt32(cblStandard.Items[i].Value);
                        cmdS.Parameters.AddWithValue("@HolidayID", NewRecordID);
                        cmdS.Parameters.AddWithValue("@StandatdID", StdId);
                        if (conn.State == ConnectionState.Open)
                        {
                            conn.Close();
                        }
                        conn.Open();
                        int res = cmdS.ExecuteNonQuery();
                        if (res > 0)
                        {

                        }
                    }
                }

Tell me what is missing? 告诉我缺少了什么?

You are using same SqlCommnad object for multiple insertions so previously added parameters are also present. 您正在使用同一个SqlCommnad object进行多次插入,因此还存在previously添加的parameters

So either create a new SqlCommnad object inside loop or clear prevoius parameters. 因此,要么在循环内创建一个新的SqlCommnad object ,要么clear prevoius参数。

This is how you can Clear Previously added parameters. 这样可以Clear以前添加的参数。

cmdS.Parameters.Clear();

You are adding parameters in a loop. 您正在循环添加参数。 So after second iteration, your command has 4 parameters. 因此,在第二次迭代之后,您的命令具有4个参数。

Each time You are adding cmdS.Parameters.AddWithValue in a loop. 每次在循环中添加cmdS.Parameters.AddWithValue。 So after the first iteration, it has already 2 parameters. 因此,在第一次迭代之后,它已经有2个参数。

You need to clear the command parameters by cmdS.Parameters.Clear() before entering the loop. 进入循环之前,需要通过cmdS.Parameters.Clear()清除命令参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM