0

我正在构建 vb.net-ms 访问数据库项目。在其中一个Insert命令中,我收到"Please check the number"指向ExecuteNonQuery代码行的异常。

这是我的代码:

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    If cbMachine.SelectedIndex <> -1 And _
       cbShift.SelectedIndex <> -1 And _
       cbWdiv.SelectedIndex <> -1 And _
       cbChky.SelectedIndex <> -1 And _
       cbBukby.SelectedIndex <> -1 And _
       cbRemarks.SelectedIndex <> -1 Then
        cmdInsert.CommandText = "INSERT INTO buktab ([Booking Date],[Machine Type],[Shift],[MS Division / Ward], Chowky, [Booked By], [Site Address], [Remarks]) VALUES(" &
            DateTimePicker1.Value.Date & ", '" & cbMachine.Text & "', '" & cbShift.Text & "', '" & cbWdiv.Text & "', '" & cbChky.Text & "', '" & cbBukby.Text & "', '" & cbSite.Text & "', '" & cbRemarks.Text & "');"

        cmdInsert.CommandType = CommandType.Text
        cmdInsert.Connection = cnnOLEDB
        cmdInsert.ExecuteNonQuery()

        MsgBox("Machine Booked Successfully!")
        'Resetting all comboboxes
        DateTimePicker1.Text = Today()
        cbMachine.SelectedIndex = -1
        cbShift.SelectedIndex = -1
        cbWdiv.SelectedIndex = -1
        cbChky.SelectedIndex = -1
        cbBukby.SelectedIndex = -1
        cbSite.Text = "Please Type Manually"
        cbRemarks.SelectedIndex = -1
    Else
        MsgBox(" Please Enter the Missing Values")
    End If
    cmdInsert.Dispose()
End Sub

请让我知道我哪里出错了。

4

1 回答 1

0

说真的,帮自己一个忙并使用参数化查询,就像这样

cmdInsert.Connection = cnnOLEDB
cmdInsert.CommandType = CommandType.Text
cmdInsert.CommandText = _
        "INSERT INTO buktab ([Booking Date],[Machine Type],[Shift],[MS Division / Ward], Chowky, [Booked By], [Site Address], [Remarks]) " & _
        "VALUES(?, ?, ?, ?, ?, ?, ?, ?)"
cmdInsert.Parameters.AddWithValue("?", DateTimePicker1.Value.Date)
cmdInsert.Parameters.AddWithValue("?", cbMachine.Text)
cmdInsert.Parameters.AddWithValue("?", cbShift.Text)
cmdInsert.Parameters.AddWithValue("?", cbWdiv.Text)
cmdInsert.Parameters.AddWithValue("?", cbChky.Text)
cmdInsert.Parameters.AddWithValue("?", cbBukby.Text)
cmdInsert.Parameters.AddWithValue("?", cbSite.Text)
cmdInsert.Parameters.AddWithValue("?", cbRemarks.Text)
cmdInsert.ExecuteNonQuery()

重新编辑:“请检查号码”异常

启动 Access 并在设计视图中打开表。检查您尝试插入的每个字段的Validation Rule和属性。Validation Text我怀疑其中一个字段的(相当无用的)Validation Text消息设置为“请检查号码”。

于 2013-11-13T17:07:55.830 回答