SQL Server:存储过程和 ASP.NET



我这里有这段代码:

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();
    }
}

我有 3 个类项目。当它第一次循环时,一切都按预期工作,但是在第二次循环后,我收到此错误:

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

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

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

例如

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

每次遍历循环时,都会添加更多参数,而您当前未清除它们。所以错误是不言自明的:指定的参数太多。

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

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

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

这样做的好处是,您不必每次都构建参数对象,在要重用对象时频繁创建和销毁对象......

最新更新