2011-12-14 39 views
0

这太简单了,我一定很愚蠢!只填写来自数据源的更改的ado.net数据集

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

我试图让DataGridView显示数据到达时。

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

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

不是我梦想的,但功能,如果完全次优。

但是,我的“解决方案”是一个烂摊子,即使屏幕上已经有599个,使用fill从数据库中提取每一条记录。

真的,我只想填写一次数据表,并在他们到达时添加新记录(或点击需要时)。

如果您还可以解释另一种方法(不经常调用该方法)来隐藏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页面(如果你知道它在哪里,我承认我觉得不适合细读,我经常会迷路)。

回答

0

假设你的ID是连续的,我会接近这个方法是:

1)记录您检索

2)当用户按下查看,只拿到ID分别为更大记录的最后一个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 
+0

大,这是真的清楚。 oDataTable中的o代表什么?另外,m_wLastID(具体来说)是什么意思? – bluekeys 2011-12-14 21:00:36