0

因此,当对数据库进行查询(两次)并在数据集中返回结果时,我有一个函数。它检查结果(以确保有一些结果)然后循环并从返回的数据集中抓取每一行并将其导入(复制)到不同的数据集。

我还将主键添加到列表中,因此我不会将相同的项目两次(来自第二个查询)添加到数据集中。

然后我返回数据集。

问题?查询有效并且有一个返回值。但是我打算将行导入到的数据集不保留导入的行。

请就我的方法和手头的问题提出任何建议;我一直在寻求改进!

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
4

1 回答 1

0

列名必须明确说明;出于某种原因,我认为数据集会隐式地从导入的数据行中继承列名。

clientsWithMonitors.Tables(0).Columns.Add("Foo")
clientsWithMonitors.Tables(0).Columns.Add("Bar")
于 2012-05-10T13:14:08.273 回答