我的简单C#应用程序出了问题。当我循环datagridview
行计数,然后相应地插入数据,它会在循环后抛出以下错误,直到58:
传入的请求包含太多参数......
但是我已经用循环和查询检查了几次,看起来似乎没有什么不对。
for (int i = 0; i < dailyReportGridView.Rows.Count - 1; i++)
{
StrQuery =
@"INSERT INTO TIME_ATTENDANCE_REPORT VALUES (@CHECKTIME" + i + ", @BADGENUMBER" + i + ", @NAME" + i +
", @IN1" + i + ", @OUT1" + i + ", @IN2" + i + ", @OUT2" + i + ", @IN3" + i + ", @OUT3" + i +
", @IN4" + i + ", @OUT4" + i + ", @IN5" + i + ", @OUT5" + i + ", @WORKDAY" + i + ", @OT" + i + ", @SUNDAYHOUR" + i +
", @SUNDAYOT" + i + ", @PHWORK" + i + ", @PHOT" + i + ", @LEAVE" + i + ", @REMARK" + i + ", @LEAVENAME" + i + ", @DEPT" + i + ", @STARTSPECDAY" + i +
", @ENDSPECDAY" + i + ", @EDITED" + i + ", @EDITREASON" + i + ", @DEDUCTION" + i + ", @DOUBLESHIFT" + i +
", @TOTALAL" + i + ", @TOTALUL" + i + ", @TOTALSL" + i + ", @TOTALOL" + i + ", @TOTALML" + i + ", @TOTALBT" + i + ", @SYNCBY" + i + ")";
comm.Parameters.Add("@CHECKTIME" + i, SqlDbType.DateTime);
comm.Parameters["@CHECKTIME" + i].Value = dailyReportGridView.Rows[i].Cells["DATE"].Value;
comm.Parameters.Add(new SqlParameter("BADGENUMBER" + i, dailyReportGridView.Rows[i].Cells["BADGENUMBER"].Value));
comm.Parameters.Add(new SqlParameter("NAME" + i, dailyReportGridView.Rows[i].Cells["NAME"].Value));
comm.Parameters.Add(new SqlParameter("IN1" + i, dailyReportGridView.Rows[i].Cells["IN_1"].Value));
comm.Parameters.Add(new SqlParameter("OUT1" + i, dailyReportGridView.Rows[i].Cells["OUT_1"].Value));
comm.Parameters.Add(new SqlParameter("IN2" + i, dailyReportGridView.Rows[i].Cells["IN_2"].Value));
comm.Parameters.Add(new SqlParameter("OUT2" + i, dailyReportGridView.Rows[i].Cells["OUT_2"].Value));
comm.Parameters.Add(new SqlParameter("IN3" + i, dailyReportGridView.Rows[i].Cells["IN_3"].Value));
comm.Parameters.Add(new SqlParameter("OUT3" + i, dailyReportGridView.Rows[i].Cells["OUT_3"].Value));
comm.Parameters.Add(new SqlParameter("IN4" + i, dailyReportGridView.Rows[i].Cells["IN_4"].Value));
comm.Parameters.Add(new SqlParameter("OUT4" + i, dailyReportGridView.Rows[i].Cells["OUT_4"].Value));
comm.Parameters.Add(new SqlParameter("IN5" + i, dailyReportGridView.Rows[i].Cells["IN_5"].Value));
comm.Parameters.Add(new SqlParameter("OUT5" + i, dailyReportGridView.Rows[i].Cells["OUT_5"].Value));
comm.Parameters.Add(new SqlParameter("WORKDAY" + i, dailyReportGridView.Rows[i].Cells["WorkDay"].Value));
comm.Parameters.Add(new SqlParameter("OT" + i, dailyReportGridView.Rows[i].Cells["OT"].Value));
comm.Parameters.Add(new SqlParameter("SUNDAYHOUR" + i, dailyReportGridView.Rows[i].Cells["SundayHour"].Value));
comm.Parameters.Add(new SqlParameter("SUNDAYOT" + i, dailyReportGridView.Rows[i].Cells["SundayOT"].Value));
comm.Parameters.Add(new SqlParameter("PHWORK" + i, dailyReportGridView.Rows[i].Cells["PH(hr)"].Value));
comm.Parameters.Add(new SqlParameter("PHOT" + i, dailyReportGridView.Rows[i].Cells["PH OT(hr)"].Value));
comm.Parameters.Add(new SqlParameter("LEAVE" + i, dailyReportGridView.Rows[i].Cells["Leave"].Value));
comm.Parameters.Add(new SqlParameter("REMARK" + i, dailyReportGridView.Rows[i].Cells["Remark"].Value));
comm.Parameters.Add(new SqlParameter("LEAVENAME" + i, dailyReportGridView.Rows[i].Cells["LeaveName"].Value));
comm.Parameters.Add(new SqlParameter("DEPT" + i, dailyReportGridView.Rows[i].Cells["Dept"].Value));
comm.Parameters.Add(new SqlParameter("STARTSPECDAY" + i, dailyReportGridView.Rows[i].Cells["StartSpecDay"].Value));
comm.Parameters.Add(new SqlParameter("ENDSPECDAY" + i, dailyReportGridView.Rows[i].Cells["EndSpecDay"].Value));
comm.Parameters.Add(new SqlParameter("EDITED" + i, dailyReportGridView.Rows[i].Cells["Edited"].Value));
comm.Parameters.Add(new SqlParameter("EDITREASON" + i, dailyReportGridView.Rows[i].Cells["Edit Reason"].Value));
comm.Parameters.Add(new SqlParameter("DEDUCTION" + i, dailyReportGridView.Rows[i].Cells["Deduction"].Value));
comm.Parameters.Add(new SqlParameter("DOUBLESHIFT" + i, dailyReportGridView.Rows[i].Cells["Double Shift"].Value));
comm.Parameters.Add(new SqlParameter("TOTALAL" + i, dailyReportGridView.Rows[i].Cells["TotalAL"].Value));
comm.Parameters.Add(new SqlParameter("TOTALUL" + i, dailyReportGridView.Rows[i].Cells["TotalUL"].Value));
comm.Parameters.Add(new SqlParameter("TOTALSL" + i, dailyReportGridView.Rows[i].Cells["TotalSL"].Value));
comm.Parameters.Add(new SqlParameter("TOTALOL" + i, dailyReportGridView.Rows[i].Cells["TotalOL"].Value));
comm.Parameters.Add(new SqlParameter("TOTALML" + i, dailyReportGridView.Rows[i].Cells["Maternity"].Value));
comm.Parameters.Add(new SqlParameter("TOTALBT" + i, dailyReportGridView.Rows[i].Cells["BusinessTrip"].Value));
comm.Parameters.Add(new SqlParameter("SYNCBY" + i, lblUserName.Text.ToString()));
comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
错误在代码行comm.ExecuteNonQuery()
上。解决它的任何解决方案?
答案 0 :(得分:4)
在comm.Parameters.Clear();
之后添加comm.ExecuteNonQuery()
- 它应该在每次迭代时执行。否则,您可以多次添加参数。