列名或提供的值数量与 table 定义不匹配 - 无法确定根本原因

Column name or number of supplied values does not match table definition - Unable to identify the root cause

获取错误

cmd.ExecuteNonQuery()

我的当前代码

Using con As New SqlConnection(sConString)
    Using cmd As New SqlCommand(
        "INSERT INTO MC_Entry VALUES(" &
        "@0,@1, @2, @3, @4, @5,@6,@7,@8,@9,@10,@11,@12,@13,@14,@15,@16,@17," &
        "@18, @19, @20, @21, @22,@23,@24,@25,@26,@27,@28,@29,@30,@31,@32,@33,@34," &
        "@35, @36, @37, @38, @39,@40,@50,@51,@52,@53,@54)", con)

        For MyIncremental = 0 To 54
            cmd.Parameters.AddWithValue("@" & MyIncremental, vValues(MyIncremental))
        Next MyIncremental

        'Debug.Print(UBound(vValues))
        'Debug.Print(LBound(vValues))
        'Debug.Print(Join(vValues, vbTab))


        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    End Using
End Using

vValues 的下限值 = 0

vValues 的上限值 = 54

我的 SQL 服务器 table 中有 55 列,没有增量字段,每个字段都可以接受 Null 值。

不确定为什么会出现此错误,因为一切似乎都正常...

Column name or number of supplied values does not match table definition.

有什么建议吗?

根据答案post,我终于循环了,这样我以后就不会错过必要的字符串了。

For i = 0 To 54
    ReDim Preserve sfields(0 To i)
    sfields(i) = "@" & i
Next

sConcat = Join(sfields, ",")

Using con As New SqlConnection(sConString)
    Using cmd As New SqlCommand("INSERT INTO MC_Entry VALUES(" & sConcat & ")", con)

        For i = 0 To 54
            cmd.Parameters.AddWithValue("@" & i, vValues(i))
        Next i

        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    End Using
End Using

您在 table 中总共有 55 列并且您缺少从 @41@49 的参数,这导致了错误。

像下面这样包含将解决您的问题

"INSERT INTO MC_Entry VALUES(" &
"@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17," &
"@18, @19, @20, @21, @22, @23, @24, @25, @26, @27, @28, @29, @30, @31, @32, @33," &
"@34, @35, @36, @37, @38, @39, @40, @41, @42, @43, @44, @45, @46, @47, @48, @49," &
"@50, @51, @52, @53, @54)", con)