仅使用数据源中的更改填充ado.net数据集

时间:2011-12-14 20:05:32

标签: vb.net ado.net

这很简单,我一定是个傻瓜!

我有一个简单的访问数据库,日志记录每小时写几次。

我正在尝试创建一个DataGridView,显示数据到达时。

我的“解决方案”很简单;

当用户点击视图时 - >从数据库中读取(填充数据表) - >更新视图。

不是我梦寐以求的,但功能性,如果完全不是最佳的。

然而,我的“解决方案”是一个dud,使用fill从数据库中绘制每一条记录,即使屏幕上已有599。

真的,我只想填充数据表一次,并在它们到达时添加新记录(或者如果需要,可以点击)。

如果您还可以解释另一种方式(不经常调用)来隐藏ID列,并将第1列(名为DateTimeStamp)的标题更改为TimeStamp,那么

奖励点。

Public Class FormMain

    Shared dataAdapter As OleDbDataAdapter
    Shared logTable As New DataTable("log")
    Shared commandBuilder As OleDbCommandBuilder
    Shared queryString As String = "SELECT * FROM log"
    Shared bindingSource As New BindingSource

    Private Sub FormServerBridge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            ConfigureDataSet()
            ConfigureBindingSource()
            ConfigureDataView()
        Catch ex As Exception
            ' FIXME: Helpful for debugging purposes but awful for the end-user.
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub ConfigureDataSet()
        dataAdapter = New OleDbDataAdapter(queryString, _Config.ConnectionString)
        commandBuilder = New OleDbCommandBuilder(dataAdapter)
        commandBuilder.GetUpdateCommand()

        dataAdapter.Fill(logTable)

        With logTable
            .Locale = System.Globalization.CultureInfo.InvariantCulture
            .PrimaryKey = New DataColumn() {logTable.Columns("ID")}
        End With
    End Sub

    Private Sub ConfigureBindingSource()
        With bindingSource
            .DataSource = logTable
        End With
    End Sub

    Private Sub ConfigureDataView()
        With DataGridView
            .DataSource = bindingSource
        End With
    End Sub

    Private Sub DataGridView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView.Click
        UpdateUI()
    End Sub

    Sub UpdateUI()
        dataAdapter.Fill(logTable)
    End Sub

    Private Sub DataGridView_DataBindingComplete(ByVal sender As Object, ByVal e As DataGridViewBindingCompleteEventArgs) Handles DataGridView.DataBindingComplete

        ' FIXME: This code gets run as many times as there are rows after dataAdapter.Fill!

        With DataGridView
            .Columns("ID").Visible = False

            .Columns(1).HeaderText = "Timestamp"

            .Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        End With

    End Sub
End Class

P.S。链接到网站和书籍将是赞赏,甚至是正确的MSDN页面(如果你知道它在哪里,我承认我觉得不舒服,仔细阅读,我经常迷路)。

1 个答案:

答案 0 :(得分:0)

假设您的ID是顺序的,我接近这个的方式是:

1)记录您检索的最后一个ID

2)当用户按下视图时,只获取ID大于最后一条记录的记录

3)将记录检索到新的数据表中,然后将其与现有数据集合并。

以下是我将如何进行更改(仅包含更改的信息):

Public Class FormMain

    Shared logTable As DataTable
    Shared bindingSource As New BindingSource

    Private m_wLastID As Integer

    Private Sub FormServerBridge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            ConfigureDataSet()
            ConfigureBindingSource()
            ConfigureDataView()
        Catch ex As Exception
            ' FIXME: Helpful for debugging purposes but awful for the end-user.
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub ConfigureDataSet()

        Dim queryString As String

        queryString = "SELECT * FROM log WHERE ID > " & m_wLastID.ToString & " ORDER BY ID"

        Using dataAdapter As New OleDbDataAdapter(queryString, _Config.ConnectionString)
            Using commandBuilder As New OleDbCommandBuilder(dataAdapter)
                Dim oDataTable As New DataTable("log")

                commandBuilder.GetUpdateCommand()

                dataAdapter.Fill(oDataTable)

                With oDataTable
                    .Locale = System.Globalization.CultureInfo.InvariantCulture
                    .PrimaryKey = New DataColumn() {.Columns("ID")}
                End With

                ' Record the last id
                If oDataTable.Rows.Count <> 0 Then
                    m_wLastID = CInt(oDataTable.Rows(oDataTable.Rows.Count - 1)("ID"))
                End If

                If logTable Is Nothing Then
                    logTable = oDataTable
                Else
                    logTable.Merge(oDataTable, True)
                    logTable.AcceptChanges()
                End If
            End Using
        End Using
    End Sub

    Sub UpdateUI()
        ConfigureDataSet()
    End Sub

    ' Rest of the form code here