Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从VB.NET使用ms access中的“自动编号”字段更新查询_Vb.net_Ms Access - Fatal编程技术网

从VB.NET使用ms access中的“自动编号”字段更新查询

从VB.NET使用ms access中的“自动编号”字段更新查询,vb.net,ms-access,Vb.net,Ms Access,如何从VB.NET使用ms access中的“自动编号”字段更新数据。我尝试使用此代码进行更新。但一切都没有改变 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

如何从VB.NET使用ms access中的“自动编号”字段更新数据。我尝试使用此代码进行更新。但一切都没有改变

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\u ID
。实际上,标签隐藏在表单中。我尝试上面的代码。但数据库中没有任何变化。请检查我的代码并更正。

问题在于您声明参数的顺序。
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)

问题在于您声明参数的顺序。
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)