2011-12-14 57 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