如何刷新datagrid和更新数据库?

时间:2013-05-07 16:25:52

标签: vb.net updating

我在更新数据库时遇到了一些麻烦。我有这个代码:

Dim cmd As OleDbCommand
        Dim sql As String
        sql = "UPDATE nmat SET nip = '" & lblNipDosen.Text & "', nim = '" & TxtNIM.Text & "', ntugas = '" & TxtNtugas.Text & "', nabsensi = '" & TxtNabsen.Text & "', nuts = '" & TxtNuts.Text & "', nuas = '" & TxtNuas.Text & "' WHERE nim='" & TxtNIM.Text & "'"

        conn.Open()
        Dim reader As OleDbDataReader

        Try
            cmd = New OleDbCommand(sql, conn)
            cmd.ExecuteNonQuery()

            DataGridView1.Refresh()

        Finally
            'reader.Close()
        End Try
        conn.Close()

我的问题是,通过这些代码,我可以在关闭并再次打开此表单后刷新网格。但是,如果我检查我的ms Access,数据根本没有登记。如何更新我的数据库并在我的datagrid中显示?

1 个答案:

答案 0 :(得分:0)

试试这个:

    Dim cmd As OleDbCommand
    Dim rstTable As New DataTable()
            Dim sql As String
            sql = "UPDATE nmat SET nip = '" & lblNipDosen.Text & "', nim = '" & TxtNIM.Text & "', ntugas = '" & TxtNtugas.Text & "', nabsensi = '" & TxtNabsen.Text & "', nuts = '" & TxtNuts.Text & "', nuas = '" & TxtNuas.Text & "' WHERE nim='" & TxtNIM.Text & "'"

            conn.Open()
            Dim reader As OleDbDataReader

            Try
                cmd = New OleDbCommand(sql, conn)
                rstTable.Load(cmd.ExecuteReader())
                DataGridView1.DataSource =rstTable

            Finally
                'reader.Close()
            End Try
            conn.Close()

你的gridview列应该绑定到一个字段