SQL Server:存储过程和ASP.NET

时间:2022-09-20 23:13:37

I have this code here:

我在这里有这个代码:

public List<CellModel> PostScheduledTasks(List<CellModel> cells)
{
    var sortedCells = cells.OrderBy(c => c.sortOrder).ToList();

    try
    {
        using (connection = new SqlConnection(connectionString))
        {
            connection.Open();

            using (SqlCommand command = new SqlCommand("PostScheduledTasks", connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                for (int i = 0; i < sortedCells.Count; i++)
                {
                    SqlParameter parameter1 = new SqlParameter("@actualStart", SqlDbType.DateTime);
                    parameter1.Value = sortedCells[i].actualDate;
                    parameter1.Direction = ParameterDirection.Input;
                    command.Parameters.Add(parameter1);

                    SqlParameter parameter2 = new SqlParameter("@actualFinish", SqlDbType.DateTime);
                    parameter2.Value = sortedCells[i].finishedDate;
                    parameter2.Direction = ParameterDirection.Input;
                    command.Parameters.Add(parameter2);

                    SqlParameter parameter3 = new SqlParameter("@actualEndDate", SqlDbType.DateTime);
                    parameter3.Value = sortedCells[i].finishedDate;
                    parameter3.Direction = ParameterDirection.Input;
                    command.Parameters.Add(parameter3);

                    SqlParameter parameter4 = new SqlParameter("@UserDate1", SqlDbType.DateTime);
                    parameter4.Value = sortedCells[i].scheduledDate;
                    parameter4.Direction = ParameterDirection.Input;
                    command.Parameters.Add(parameter4);

                    SqlParameter parameter5 = new SqlParameter("@IsCompleted", SqlDbType.Bit);
                    parameter5.Value = (sortedCells[i].selected == true) ? 1 : 0;
                    parameter5.Direction = ParameterDirection.Input;
                    command.Parameters.Add(parameter5);

                    SqlParameter parameter6 = new SqlParameter("@PercentComplete", SqlDbType.Float);
                    parameter6.Value = (sortedCells[i].selected == true) ? 1 : 0;
                    parameter6.Direction = ParameterDirection.Input;
                    command.Parameters.Add(parameter6);

                    SqlParameter parameter7 = new SqlParameter("@UStmp", SqlDbType.VarChar);
                    parameter7.Value = sortedCells[i].completedBy;
                    parameter7.Direction = ParameterDirection.Input;
                    command.Parameters.Add(parameter7);

                    SqlParameter parameter8 = new SqlParameter("@ScheduleTaskID", SqlDbType.Int);
                    parameter8.Value = sortedCells[i].scheduleTaskID;
                    parameter8.Direction = ParameterDirection.Input;
                    command.Parameters.Add(parameter8);

                    SqlParameter parameter9 = new SqlParameter("@SortOrder", SqlDbType.Int);
                    parameter9.Value = sortedCells[i].sortOrder;
                    parameter9.Direction = ParameterDirection.Input;
                    command.Parameters.Add(parameter9);

                    command.ExecuteNonQuery();
                }

                UserModel userModel = new UserModel();
                userModel.name = "true";
                userModel.userName = "true";

                return cells;
            }
        }
    }
    catch(Exception e)
    {
        var error = e.Message.ToString();

        UserModel nullModel = new UserModel();
        nullModel.name = "true";
        nullModel.userName = "true";

        return cells;
    }
    finally
    {
        connection.Close();
    }
}

And I have 3 class items. When it does the loop the first time, everything works as expected, but after the second time around the loop, I get this error:

我有3个课程。当它第一次执行循环时,一切都按预期工作,但在循环第二次之后,我收到此错误:

Procedure or function PostScheduledTasks has too many arguments specified.

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

Do I have to clear something at the end of the loop after each item?

在每个项目之后,我是否必须在循环结束时清除某些内容?

2 个解决方案

#1


3  

You should clear the parameters held in command.Parameters property by calling Clear() method before each iteration.

您应该在每次迭代之前通过调用Clear()方法清除command.Parameters属性中保存的参数。

E.g.

例如。

for (int i = 0; i < sortedCells.Count; i++)
{
  command.Parameters.Clear();
  //your code to add parameters
}

Every time you go through the loop more parameters are added and you are not currently clearing them. So the error is self-explanatory: too many parameters specified.

每次进行循环时,都会添加更多参数,并且您当前没有清除它们。所以错误是不言自明的:指定的参数太多。

#2


1  

Define the parameters first, then put the for loop after that and set the values directly; currently, if you read through your code, the command has 9 parameters on the first iteration, but 18 on the second, 27 on the third and so on.

首先定义参数,然后在之后放置for循环并直接设置值;目前,如果您通读代码,该命令在第一次迭代时有9个参数,但在第二次迭代时有18个,在第三次迭代时有27个,依此类推。

Change it to (rough pseudocode):

将其更改为(粗糙伪代码):

//Define parameters here

for (..)
{
  cmd.Parameters[0].Value = "X";
  .
  .
}

The benefits to doing it this way is you aren't building up the parameter objects every time, creating and destroying objects frequently when you are going to reuse them...

这样做的好处是你不是每次都建立参数对象,当你要重用它们时经常创建和销毁对象......

#1


3  

You should clear the parameters held in command.Parameters property by calling Clear() method before each iteration.

您应该在每次迭代之前通过调用Clear()方法清除command.Parameters属性中保存的参数。

E.g.

例如。

for (int i = 0; i < sortedCells.Count; i++)
{
  command.Parameters.Clear();
  //your code to add parameters
}

Every time you go through the loop more parameters are added and you are not currently clearing them. So the error is self-explanatory: too many parameters specified.

每次进行循环时,都会添加更多参数,并且您当前没有清除它们。所以错误是不言自明的:指定的参数太多。

#2


1  

Define the parameters first, then put the for loop after that and set the values directly; currently, if you read through your code, the command has 9 parameters on the first iteration, but 18 on the second, 27 on the third and so on.

首先定义参数,然后在之后放置for循环并直接设置值;目前,如果您通读代码,该命令在第一次迭代时有9个参数,但在第二次迭代时有18个,在第三次迭代时有27个,依此类推。

Change it to (rough pseudocode):

将其更改为(粗糙伪代码):

//Define parameters here

for (..)
{
  cmd.Parameters[0].Value = "X";
  .
  .
}

The benefits to doing it this way is you aren't building up the parameter objects every time, creating and destroying objects frequently when you are going to reuse them...

这样做的好处是你不是每次都建立参数对象,当你要重用它们时经常创建和销毁对象......