Vb.net 数据库未通过DataGridView更新
我正在尝试通过vb.net上的Vb.net 数据库未通过DataGridView更新,vb.net,winforms,ms-access,datagridview,Vb.net,Winforms,Ms Access,Datagridview,我正在尝试通过vb.net上的DataGridView更新access数据库,而无需使用向导 然而,我遇到了两个主要问题: 当我试图保存添加到网格中的内容时,一个消息框会显示try-catch中的异常,该异常会说“DataTable已属于此数据集” 当我能够毫无例外地进行更改时,数据被保存到数据库中,但是当我后来关闭一个数据库并重新打开时,DataGridView更改被撤消。请注意,数据库已放置在bin/Debug文件夹中 以下是保存事件的代码: Dim dataAdapter As New O
DataGridView
更新access数据库,而无需使用向导
然而,我遇到了两个主要问题:
“DataTable已属于此数据集”
DataGridView
更改被撤消。请注意,数据库已放置在bin/Debug文件夹中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
这将打开并向datagridview显示您的sqlQuery结果,查看它是否适用于您的终端:D如果这是VB.NET,您可能不需要access vba标记。您添加表两次,就像错误所说的那样:一次在连接后立即打开,然后再打开如果您正在引用此部分dataTable.Rows.add(newRow)DataSet.Tables.add(dataTable),则我已删除“添加表”,但问题仍然存在。
DataSet.Tables.add(dataTable)
两个位置的代码行完全相同。如果您去掉Try/Catch,它将显示导致错误的行—可能是该代码的第二个实例。除非您要处理多个数据表,否则您实际上不需要数据集(重新编辑)您的数据集是表单级对象-您不能在每次单击按钮时反复向其添加相同的数据表。关于项目符号2,保存已消失,第一个原因是数据库上的“复制到输出目录”设置。看到下面的选项,我写了,因为许多人没有意识到这一点。
> 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