ADD SQL QUERY STAT

最后都变了- 提交于 2019-12-02 10:13:19

You are trying to set an UPDATE command text for the SelectCommand of the Adapter. This, of course has no way to be successful. Last but not least the UPDATE command text doesn't contain a WHERE clause, and so, if executed, it updates every record in the table tbl_empinfo with the same data.

In this context there is no need to use an adapter. You could simply execute the command, providing an appropriate WHERE clause and the values for the other parameters

   Dim sqlQuery As String = "UPDATE tbl_empinfo " & _
        "SET FirstName=?, LastName=?, Department=?, " & _
        "Status=?, Years=? " & _
        "WHERE empID=?"

    Dim cmd As New OleDbCommand
    With cmd
        .CommandText = sqlQuery
        .Connection = con
        .Parameters.AddWithValue("@p1", empFName)
        .Parameters.AddWithValue("@p2", empLName)
        .Parameters.AddWithValue("@p3", empDept)
        .Parameters.AddWithValue("@p4", empStat)
        .Parameters.AddWithValue("@p5", empYears)
        .Parameters.AddWithValue("@p6", empNum)
        .ExecuteNonQuery()
    End With

After this you could call the code that reloads the data to fill your grid

    sqlQuery = "SELECT * FROM tbl_empinfo"
    Dim cmd1 As New OleDbCommand
    Dim da As New OleDbDataAdapter
    Dim Table As New DataTable

    With cmd1
        .CommandText = sqlQuery
        .Connection = con
        With da
            .SelectCommand = cmd1
            .Fill(Table)
        End With
        With DataGridView1
            .DataSource = Table
        End With
    End With

Notice that I have changed the name of the command and the adapter to a less confusing names. SqlCommand And SqlDataAdapter are the names of the classes equivalent to the OleDbCommand and OleDbDataAdapter but used for Sql Server. At first sight I was thinking that you were trying to use the SqlClient classes to update an MS-Access database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!