Procedure or function has too many arguments specified

后端 未结 2 1188
时光取名叫无心
时光取名叫无心 2021-01-18 03:05

I get an error

Procedure or function \"myStoreProcNameHere\" has too many arguments specified.

Below is my stored procedure and

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-18 04:02

    I've ran into this error message before when I was calling a stored procedure from within a loop (foreach).

    The cause of the exception that I received was due to the fact that I wasn't clearing the Parameter's after each iteration of the loop.

    For example: This code throws:

    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.CommandText = "MySproc";
        foreach (MyClass c in myClasses)
        {
            cmd.Parameters.AddWithValue("@val1", c.val1);
            cmd.Parameters.AddWithValue("@val2", c.val2);
            var result = MyDbManager.instance.ExecuteScalarQuery(cmd);
        }
    }
    

    The fix was to clear the parameters:

    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.CommandText = "MySproc";
        foreach (MyClass c in myClasses)
        {
            cmd.Parameters.AddWithValue("@val1", c.val1);
            cmd.Parameters.AddWithValue("@val2", c.val2);
            var result = MyDbManager.instance.ExecuteScalarQuery(cmd);
            cmd.Parameters.Clear();
        }
    }
    

提交回复
热议问题