更新语句 C# Microsoft Access 中没有为一个或多个必需参数给出值

问题描述 投票:0回答:1

我不断收到此错误,并检查了很多次以确保一切是否正确。我认为我做的一切都是正确的。我不确定问题出在哪里。

_query = string.Format("更新 tblFormOQ SET UINumWorkers1 = {0},UINumWorkers2 = {1},UINumWorkers3 = {2},UISubjectWages = {3},UIExcessWages = {4},UITaxRate = {5},UIPrepaid = { 6}、UIPenalty = {7}、SWTSubjectWages = {8}、 SWTax = {9},SWTPrepaid = {10},SWTMonth1 = {11},SWTMontumh2 = {12},SWTMonth3 = {13},TriMetSubjectWages = {14},TriMetPrepaid = {15},LaneSubjectWages = {16},LanePrepaid = {17},WC 小时 = {18},WCPrepaid = {19} 其中BusinessID = '{20}',年份 = '{21}',季度 = {22}"、_UINumWorkers1、_UINumWorkers2、_UINumWorkers3、_UISubjectWages、_UIExcessWages、_UITaxRate、_UIPrepaid、_UIPenalty、_SWTSubjectWages、_SWTax、_SWTPrepaid、 _SWTMonth1、_SWTMonth2、_SWTMonth3、_TriMetSubjectWages、_TriMetPrepaid、_LaneSubjectWages、_LanePrepaid、_WCHours、_WCPrepaid、_Businessid、_Year、_Quarter);

c# ms-access windows
1个回答
1
投票

你有两个问题:

首先,

Year
是Access SQL中的保留字,因此您需要将其括在方括号中才能将其用作列名。

此外,您正在使用“动态 SQL”来构建命令,因此如果您的数据包含特殊字符(或者如果您忘记了分隔符,就像这里的情况一样),它可能会中断。你真的应该使用参数化查询,像这样

using (OleDbCommand cmd = new OleDbCommand())
{
    cmd.Connection = conn;
    cmd.CommandText = "UPDATE tblFormOQ SET UINumWorkers1 = ?,UINumWorkers2 = ?, UINumWorkers3 = ?, UISubjectWages = ?, UIExcessWages= ?, UITaxRate = ?, UIPrepaid = ?, UIPenalty = ?, SWTSubjectWages = ?, SWTax = ?, SWTPrepaid = ?, SWTMonth1 = ?, SWTMonth2 = ?, SWTMonth3 = ?, TriMetSubjectWages = ?, TriMetPrepaid = ?, LaneSubjectWages = ?, LanePrepaid = ?, WCHours = ?, WCPrepaid = ? WHERE BusinessID = ? and [Year] = ? and Quarter = ?";
    cmd.Parameters.AddWithValue("?", _UINumWorkers1);
    cmd.Parameters.AddWithValue("?", _UINumWorkers2);
    cmd.Parameters.AddWithValue("?", _UINumWorkers3);
    cmd.Parameters.AddWithValue("?", _UISubjectWages);
    cmd.Parameters.AddWithValue("?", _UIExcessWages);
    cmd.Parameters.AddWithValue("?", _UITaxRate);
    cmd.Parameters.AddWithValue("?", _UIPrepaid);
    cmd.Parameters.AddWithValue("?", _UIPenalty);
    cmd.Parameters.AddWithValue("?", _SWTSubjectWages);
    cmd.Parameters.AddWithValue("?", _SWTax);
    cmd.Parameters.AddWithValue("?", _SWTPrepaid);
    cmd.Parameters.AddWithValue("?", _SWTMonth1);
    cmd.Parameters.AddWithValue("?", _SWTMonth2);
    cmd.Parameters.AddWithValue("?", _SWTMonth3);
    cmd.Parameters.AddWithValue("?", _TriMetSubjectWages);
    cmd.Parameters.AddWithValue("?", _TriMetPrepaid);
    cmd.Parameters.AddWithValue("?", _LaneSubjectWages);
    cmd.Parameters.AddWithValue("?", _LanePrepaid);
    cmd.Parameters.AddWithValue("?", _WCHours);
    cmd.Parameters.AddWithValue("?", _WCPrepaid);
    cmd.Parameters.AddWithValue("?", _Businessid);
    cmd.Parameters.AddWithValue("?", _Year);
    cmd.Parameters.AddWithValue("?", _Quarter);
    cmd.ExecuteNonQuery();
}
© www.soinside.com 2019 - 2024. All rights reserved.