【问题标题】:No value given for one or more required parameters when using the `UPDATE` command使用 `UPDATE` 命令时没有为一个或多个必需参数指定值
【发布时间】:2016-09-19 00:49:04
【问题描述】:

我正在尝试使用UPDATE 命令更新我的记录。我收到此错误:

没有为一个或多个必需参数指定值。

这是我的代码:

 Private Sub saveOle_Click(sender As Object, e As EventArgs) Handles saveOle.Click
    Try
        Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\samplelangtowalangya.mdb;")
            conn.Open()
            Dim command As New OleDbCommand("UPDATE sample SET ewan = @ewan, ko = @ko, sayo = @sayo, hehehe = @hehehe WHERE Number_of_Employees = @Number_of_Employees", conn)
            With command.Parameters
                .AddWithValue("@ewan", ewan.Text)
                .AddWithValue("@ko", ko.Text)
                .AddWithValue("@sayo", sayo.Text)
                .AddWithValue("@hehehe", hehehe.Text)
                command.ExecuteNonQuery()
            End With

            MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            command.Dispose()
            conn.Close()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

Number_of_Employees 是我的 ID 和主键。

【问题讨论】:

  • SQL 有 5 个命名参数,而您只提供 4 个。您需要在 where 子句中为命名值提供一个值,否则生成的 SQL 会是什么样子?
  • 哦,是的。我试图将ID 放在@ewan 之前。我把它放在四个字段之后。现在可以了,谢谢:)

标签: vb.net ms-access


【解决方案1】:

所以我尝试将ID 放在四个字段之后,现在效果很好:)

 Private Sub saveOle_Click(sender As Object, e As EventArgs) Handles saveOle.Click
    Try
        Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\samplelangtowalangya.mdb;")
            conn.Open()
            Dim command As New OleDbCommand("UPDATE sample SET ewan = @ewan, ko = @ko, sayo = @sayo, hehehe = @hehehe WHERE Number_of_Employees = @Number_of_Employees", conn)
            With command.Parameters
                .AddWithValue("@ewan", ewan.Text)
                .AddWithValue("@ko", ko.Text)
                .AddWithValue("@sayo", sayo.Text)
                .AddWithValue("@hehehe", hehehe.Text)
                .AddWithValue("@Number_of_Employees", IDtext.Text)
            End With
            command.ExecuteNonQuery()
            MessageBox.Show("Employee's Informations Successfuly Updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            command.Dispose()
            conn.Close()
        End Using
    Catch ex As Exception
        MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

【讨论】:

    猜你喜欢
    • 2011-01-23
    • 2017-12-25
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多