Datagridview多次加载数据库

时间:2014-04-03 11:00:37

标签: vb.net datagridview

表单上有一个datagridview和一个按钮。相同的按钮将数据加载到datagridview(如果Button1.Text =" CHANGE DATABASE"那么)并保存更改。一切正常,直到再次单击按钮加载数据,然后在上次加载后再次加载数据库(如果数据库有3行,再次显示这3行,并且每次按钮单击再次在datagridview中 - 重复但不在Access文件中在datagridview)。

我第二次点击UPDATE CHANGES数据库时出现此错误:并发冲突。

    Imports System.Data.OleDb
Public Class Form1

    Dim DataSet1 As New DataSet()

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\PD_D.mdb"
        Dim SQLString As String = "SELECT * FROM DIV"
        Dim OleDBConn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(ConnString)
        Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, OleDBConn1)

        Using OleDBConn1
            If Button1.Text = "CHANGE DATABASE" Then
                OleDbDataAdapter1.Fill(DataSet1, "DIV")
                DataGridView1.DataSource = DataSet1.Tables("DIV")

                Button1.Text = "UPDATE CHANGES"
                Button1.ForeColor = Color.DarkRed


            Else

                If DataSet1.HasChanges Then

                    Using con = New OleDbConnection(ConnString)

                        Me.Validate()
                        Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(OleDbDataAdapter1)
                        OleDbDataAdapter1.SelectCommand = New OleDbCommand(SQLString, con)
                        OleDbDataAdapter1.DeleteCommand = builder.DataAdapter.DeleteCommand
                        OleDbDataAdapter1.InsertCommand = builder.DataAdapter.InsertCommand
                        OleDbDataAdapter1.UpdateCommand = builder.DataAdapter.UpdateCommand
                        OleDbDataAdapter1.Update(DataSet1.Tables("DIV").GetChanges())


                    End Using

                Else
                    'OleDBConn1.Close()
                    GoTo ext
                End If

ext:
                '
                Button1.Text = "CHANGE DATABASE"
                Button1.ForeColor = Color.Black


            End If


        End Using

    End Sub

1 个答案:

答案 0 :(得分:2)

由于每次点击按钮时数据集的DataTable(DIV)都被填充,您将收到重复的条目。

点击按钮1中的以下行将解决它:

DataSet1 = New DataSet()

之后您的代码将如下所示:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\PD_D.mdb"
    Dim SQLString As String = "SELECT * FROM DIV"
    Dim OleDBConn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(ConnString)
    Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, OleDBConn1)

    Using OleDBConn1
        If Button1.Text = "CHANGE DATABASE" Then
            DataSet1 = New DataSet() 'Creates new instance of DataSet1
            OleDbDataAdapter1.Fill(DataSet1, "DIV")
            DataGridView1.DataSource = DataSet1.Tables("DIV")

            Button1.Text = "UPDATE CHANGES"
            Button1.ForeColor = Color.DarkRed
        Else
            If DataSet1.HasChanges Then
                Using con = New OleDbConnection(ConnString)

                    Me.Validate()
                    Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(OleDbDataAdapter1)
                    OleDbDataAdapter1.SelectCommand = New OleDbCommand(SQLString, con)
                    OleDbDataAdapter1.DeleteCommand = builder.DataAdapter.DeleteCommand
                    OleDbDataAdapter1.InsertCommand = builder.DataAdapter.InsertCommand
                    OleDbDataAdapter1.UpdateCommand = builder.DataAdapter.UpdateCommand
                    OleDbDataAdapter1.Update(DataSet1.Tables("DIV").GetChanges())
                End Using
            Else
                'OleDBConn1.Close()
                GoTo ext
            End If
ext:
                '
                Button1.Text = "CHANGE DATABASE"
                Button1.ForeColor = Color.Black
        End If
    End Using
End Sub