2012-05-10 14 views
0

所以我有一個函數,當對數據庫發起查詢(兩次)並返回數據集中的結果。它檢查結果(以確保有一些結果)然後遍歷並從返回的數據集中抓取每一行並將其導入(複製)到不同的數據集。DataSet行添加/導入沒有

我還將一個主鍵添加到列表中,所以我不會將相同的項目從第2個查詢添加到數據集。

然後我返回數據集。

問題?該查詢工作,並有一個返回的值..然而,我想在其中導入行的數據集不保留導入的行。

請對我的方法和問題提出任何建議;我一直在尋求改進!

Public Function GetClientsWithMonitors(ByVal argHost As FOO.Interfaces.BAR) As DataSet 
    Try 
     Dim localDataSet As New DataSet() 
     Dim clientsWithMonitors As New DataSet() 
     Dim tempList As New List(Of Integer) 

     clientsWithMonitors.Clear() 
     localDataSet.Clear() 
     tempList.Clear() 
     clientsWithMonitors.Tables.Add() 

     'SQL getting monitors applied to clients 
     Dim clientSQL As String = "SELECT DISTINCT c.ClientID, c.Name FROM agents a LEFT JOIN clients c ON c.ClientID = a.ClientID WHERE a.ClientID > 0" 
     'SQL getting monitors applied directly to an agent 
     Dim agentSQL As String = "SELECT DISTINCT c.ClientID, c.Name FROM clients c LEFT JOIN computers comp ON c.ClientID = comp.ClientID LEFT JOIN agents a ON comp.ComputerID = a.ComputerID WHERE a.LocID = 0 AND a.ClientID = 0 AND a.ComputerID > 0" 
     localDataSet = argHost.GetDataSet(clientSQL) 
     If localDataSet.Tables.Count > 0 AndAlso localDataSet.Tables(0).Rows.Count > 0 Then 
      For Each row As DataRow In localDataSet.Tables(0).Rows 
       If Not tempList.Contains(CInt(row("ClientID").ToString())) Then 
        Dim clientID As Integer = CInt(row("ClientID").ToString()) 
        clientsWithMonitors.Tables(0).ImportRow(row) 
        tempList.Add(clientID) 
       End If 
      Next 
     End If 
     If localDataSet.Tables.Count > 0 AndAlso localDataSet.Tables(0).Rows.Count > 0 Then 
      localDataSet.Clear() 
      localDataSet = argHost.GetDataSet(agentSQL) 
      For Each row As DataRow In localDataSet.Tables(0).Rows 
       If Not tempList.Contains(CInt(row("ClientID").ToString())) Then 
        Dim clientID As Integer = CInt(row("ClientID").ToString()) 
        clientsWithMonitors.Tables(0).ImportRow(row) 
        tempList.Add(clientID) 
       End If 
      Next 
     End If 
     Return clientsWithMonitors 
    Catch ex As Exception 
     LogEventViaHost(argHost, "Error Getting dataset of clients with a specified monitor" & ex.StackTrace & " " & ex.Message) 
     Return Nothing 
    End Try 

回答

0

列名必須明確聲明;出於某種原因,我想數據集會隱式地從導入的數據行中繼承列名。

clientsWithMonitors.Tables(0).Columns.Add("Foo") 
clientsWithMonitors.Tables(0).Columns.Add("Bar")