首先,如果要更新记录,则无需选择记录。因此,我将使用 aSqlCommand和适当的 update-sql。然后使用它的ExecuteNonQuery方法。
其次,由于您将 ID 转换为Int32我假设数据库中列的类型也是int. 但是您将string参数传递SelectCommand给DataAdapter. 请注意,AddWithValue尝试从值推断类型,因此您应该提供正确的类型。
但是,如果您想使用 aSqlDataAdapter来更新记录,您必须提供UpdateCommand. 然后,当您调用时,DataTable/DataSet 中的所有更改都将写入数据库DataAdapter.Update。
这是一个示例(同样,选择是多余且低效的,因为您可以直接使用 更新SqlCommand.ExecuteNonQuery):
Try
   Dim id As Int32
   If Not Int32.TryParse(txt_id.Text, id) Then
        MsgBox("Please enter a valid ID!")
        Return
    End If
    Using myConn = New SqlConnection("Connection String")
        Using da = New SqlDataAdapter("SELECT * FROM studentdetails where student_id= @id", myConn)
            Dim table = New DataTable()
            da.SelectCommand.Parameters.AddWithValue("@id", id)
            da.Fill(table)
            If table.Rows.Count = 0 Then
                MsgBox("Record not found")
            Else
                da.UpdateCommand = New SqlCommand("UPDATE studentdetails SET student_id=@student_id,student_name=@student_name,student_branch=@student_branch,student_class=@student_class WHERE student_id=@id", myConn)
                da.UpdateCommand.Parameters.AddWithValue("@id", id)
                da.UpdateCommand.Parameters.AddWithValue("@student_name", txt_name.Text)
                da.UpdateCommand.Parameters.AddWithValue("@student_branch", txt_branch.Text)
                da.UpdateCommand.Parameters.AddWithValue("@student_class", txt_class.Text)
                Dim row = table.Rows(0)
                row.SetField("student_id", Int32.Parse(txt_id.Text))
                row.SetField("student_name", txt_name.Text)
                row.SetField("student_branch", txt_branch.Text)
                row.SetField("student_class", txt_class.Text)
                da.Update(table)
            End If
        End Using
    End Using
Catch ex As Exception
    MsgBox(ex.Message)
Finally
    'You don't need to close the connection if you use the using-statement
    'myConn.Close()
End Try