数据库不通过DataGridView更新

时间:2015-11-11 18:18:18

标签: vb.net winforms ms-access datagridview

我正在尝试通过vb.net上的DataGridView更新访问数据库,而无需使用向导。

但是,我遇到了两个主要问题:

  1. 当我尝试保存添加到网格中的内容时,会出现一个消息框,显示来自try catch "DataTable already belong to this dataSet"的异常。

  2. 当我能够无异常地进行更改时,数据已保存到数据库中,但是当我稍后关闭并重新打开数据库并DataGridView时,更改已被撤消。请注意,数据库已放在bin / Debug文件夹中。

  3. 以下是保存事件的代码:

    Dim dataAdapter As New OleDbDataAdapter
    Dim DataTable As New DataTable
    Dim DataSet As New DataSet
    Dim Connection As New OleDbConnection
    
    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        Try
            Connection.Open() ' the following decleration are used to save content to the table.     
            dataSet.Tables.Add(dataTable)
            dataAdapter = New OleDbDataAdapter("SELECT * FROM Students", Connection)
            dataAdapter.Fill(dataTable)
            Dim newRow As DataRow = dataTable.NewRow
            With newRow ' the with statement allows you do repeatedly apply a property to a certain object
                .Item("StudentID") = txtStudentID.Text ' these statements add the content of the text boxes to these respective fields in the database
                .Item("TeacherID") = txtTeacherID.Text
                .Item("StudentFirstName") = txtStudentFirstname.Text
                .Item("StudentSurname") = txtStudentSurname.Text
                .Item("StudentPassword") = txtStudentPassword.Text
                .Item("StudentGroup") = cbxStudentGroup.Text
            End With
            dataTable.Rows.Add(newRow)
            DataSet.Tables.Add(DataTable)
            Dim Command As New OleDbCommandBuilder(dataAdapter)
            dataAdapter.Update(dataTable) 'updates the table
            Connection.Close()
            ShowItems() ' displays the table
    
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Connection.Close()
        End Try
    
    End Sub
    

    如果需要更多详细信息,请询问。 编辑:我发现第一个问题在尝试通过dataGrid删除时也很普遍,这里是代码:

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            dataSet.Tables.Add(dataTable)
            Connection.Open()
            dataAdapter = New OleDbDataAdapter("SELECT * FROM Students", Connection)
            dataAdapter.Fill(dataTable)
            dataTable.Rows(0).BeginEdit()
            dataTable.Rows(0).Delete()
            dataTable.Rows(0).EndEdit()
            Dim Commandbuilder As New OleDbCommandBuilder(dataAdapter)
            dataAdapter.Update(dataTable)
            dgrStudentDatabaseViewer.DataSource = dataTable.DefaultView
            Connection.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Connection.Close()
        End Try
    End Sub
    

1 个答案:

答案 0 :(得分:0)

>  Public Sub fillDatatable(_datatable As DataTable, Query As String)
>     Dim sqlQuery As String = (Query)
>     Using conn As New SqlConnection("YourConnectionString")
>         conn.Open()
>         Using da As New SqlDataAdapter(sqlQuery, conn)
>             da.Fill(_datatable)
>         End Using
>         conn.Close()
>     End Using End Sub

Private Sub yourEvent()
    Using xDT As New Datatable
        fillDatatable(xDT, "Your Query;")
        yourDGV.DataSource = sssCompDT
        yourDGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        yourDGV.Refresh()
    End Using
End Sub

这应该打开你的sqlQuery结果显示到datagridview,看看它是否适用于你的结尾:D