0

我如何在 VB.NET 的 ms 访问中使用 Autonumber 字段更新数据。我尝试使用此代码进行更新。但什么都没有改变。

Private Sub BTNUPDATE_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTNUPDATE.Click
    Try
        getConnect()

        Dim strSQL As String
        Dim iCount As Integer
        strSQL = " UPDATE DEPARTMENT SET [DEPART]=@DEPART,[DEP_DSCRPTN]=@DEP_DSCRPTN WHERE [DEP_ID] = @DEP_ID"
        Dim cmd As New OleDb.OleDbCommand(strSQL, Conn)
        cmd.Parameters.AddWithValue("@DEP_ID", CInt(DEPID.Text))
        cmd.Parameters.AddWithValue("@DEPART", CMBDEPT.Text)
        cmd.Parameters.AddWithValue("@DEP_DSCRPTN", TXTDESC.Text)
        Conn.Open()
        iCount = cmd.ExecuteNonQuery()
        Conn.Close()
        If iCount > 0 Then
            MessageBox.Show("Record Updated Successfully!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information)
            If Windows.Forms.DialogResult.OK Then
                BTNCLEAR.PerformClick()
            End If
        Else
            MsgBox("No record was inserted")
        End If

    Catch ex As Exception
        MsgBox("ERROR: " + ex.Message, MsgBoxStyle.Information, "Update")
    Finally

        BTNCLEAR.PerformClick()
    End Try

End Sub

在数据库DEP_ID中是自动编号。并且DEPID是要检索的标签框DEP_ID。实际上标签隐藏在表单中。我试试上面的代码。但是数据库没有任何变化。请检查我的代码并纠正我。

4

1 回答 1

3

问题在于您声明参数的顺序。
OleDb 提供程序不使用参数名称,而是使用它在集合中的位置

在您的查询中,@DEP_ID 参数是最新的,但在集合中您将其声明为第一个。
如果查询没有更新错误的记录,你是幸运的。

    strSQL = " UPDATE DEPARTMENT SET [DEPART]=@DEPART,[DEP_DSCRPTN]=@DEP_DSCRPTN WHERE [DEP_ID] = @DEP_ID"
    Dim cmd As New OleDb.OleDbCommand(strSQL, Conn)
    cmd.Parameters.AddWithValue("@DEPART", CMBDEPT.Text)
    cmd.Parameters.AddWithValue("@DEP_DSCRPTN", TXTDESC.Text)
    cmd.Parameters.AddWithValue("@DEP_ID", CInt(DEPID.Text)
于 2013-04-30T10:30:48.027 回答