如何将整个DataTable列转换为字符串? -VB.Net

时间:2018-11-28 06:09:56

标签: vb.net

在我正在从事的这个项目中,我必须使用SQL查询创建一堆饼图。

SQL有2个阶段

首先,我要收集在用户选择的时间范围内运行的计算机的名称,并将其放置在数据表中。

    Dim query1 As String = String.Format("SELECT DISTINCT A.MacID FROM dbo.tblMachine A LEFT JOIN  dbo.tblDataHdr b on a.MacID = b.MacID where DayID between '" & TextBox1.Text & "' AND '" & TextBox3.Text & "' ORDER BY A.MACID")
    Dim dt1 As DataTable = GetData(query1)

在此示例中,我收集的计算机名称为

--------------
|    MacId   |
--------------
|    A01     |
|    A02     |
|    C01     |
--------------

对于第二阶段,我想使用在第一阶段中收集的数据并将其引用到另一个SQL查询中。我的想法是将表中的内容转换为字符串,如下所示:

'A01', 'A02', 'C01' 

这样,我只需要运行第二个SQL查询一次,而不必依赖For-Next语句来获取数据,这是我上次使用的结果,导致加载时间长,尤其是当有30多台机器。

如何获得此结果?我已经考虑了2天了,不知道如何开始。

编辑:

对于某些情况,这是我当前的代码。请忽略混乱。

Sub draw_chart1() 'All Machines

    Dim check1, check2, fi, cnt
    Dim seperator As String = ", "
    Dim columnindex As Integer = 0

    Dim myConnectionString As String = "Provider=SQLOLEDB;" & SQLDB_pp.ConnectionString
    fi = 0
    cnt = 0

    PlaceHolder1.Dispose()
    PlaceHolder2.Dispose()

    'Get all machines
    Dim query1 As String = String.Format("SELECT DISTINCT A.MacID FROM dbo.tblMachine A LEFT JOIN  dbo.tblDataHdr b on a.MacID = b.MacID where DayID between '" & TextBox1.Text & "' AND '" & TextBox3.Text & "' ORDER BY A.MACID")
    Dim dt1 As DataTable = GetData(query1)

    Dim result As String = String.Join(seperator, dt1.AsEnumerable.Select(Function(r) "'" + r(columnindex).ToString() + "'")).TrimEnd(seperator.ToCharArray())

    Dim dt As DataTable
    Dim query As String


    'Dim query As String = String.Format("SELECT Z.MacID, Z.EventName, ISNULL(DIFF,0) AS DIFF FROM (select distinct MacID, EventName from dbo.tblMachine a " _
    '                                        & "join (SELECT DISTINCT EVENTNAME FROM dbo.tblEvtDur where EventName <> 'ON'  ) b on b.EventName <> ''  and MacID in ('" & check1 & "') ) Z " _
    '                                        & "LEFT JOIN (SELECT A.MacID, A.EventName, SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) as diff,round(SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) / cast(aVG(Tdiff) " _
    '                                        & "as decimal(30,8)),4) * 100 AS PER FROM dbo.tblEvtDur  A LEFT JOIN ( SELECT MacID, SUM(DATEDIFF(SECOND, STARTdt, eNDdt)) as Tdiff FROM  dbo.tblEvtDur " _
    '                                        & "WHERE DayID between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  GROUP BY MacID ) B ON  A.MacID = B.MacID WHERE DayID " _
    '                                        & "between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  AND A.MacID in ('" & check1 & "') group by A.MacID, A.EventName) a " _
    '                                        & "ON A.EVENTNAME = Z.EVENTNAME  and Z.MacID  = a.MacID order by Z.MacID, Z.EventName")
    'Dim dt As DataTable = GetData(query)

    'For q As Integer = 0 To dt1.Rows.Count - 1

    'check1 = dt1.DataSet.ToString
    'check1 = dt1.Rows(q)(0).ToString() '(0) means column, since query1 only search for 1 column, it uses 0 = 1ST COLUMN

    query = String.Format("SELECT Z.MacID, Z.EventName, ISNULL(DIFF,0) AS DIFF FROM (select distinct MacID, EventName from dbo.tblMachine a " _
            & "join (SELECT DISTINCT EVENTNAME FROM dbo.tblEvtDur where EventName <> 'ON'  ) b on b.EventName <> ''  and MacID in ({0}) ) Z " _
            & "LEFT JOIN (SELECT A.MacID, A.EventName, SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) as diff,round(SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) / cast(aVG(Tdiff) " _
            & "as decimal(30,8)),4) * 100 AS PER FROM dbo.tblEvtDur  A LEFT JOIN ( SELECT MacID, SUM(DATEDIFF(SECOND, STARTdt, eNDdt)) as Tdiff FROM  dbo.tblEvtDur " _
            & "WHERE DayID between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  GROUP BY MacID ) B ON  A.MacID = B.MacID WHERE DayID " _
            & "between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  AND A.MacID in ({0}) group by A.MacID, A.EventName) a " _
            & "ON A.EVENTNAME = Z.EVENTNAME  and Z.MacID  = a.MacID order by Z.MacID, Z.EventName", result)

    dt = GetData(query)

    'Next

    'dt = GetData(query)

    For a As Integer = 0 To dt.Rows.Count - 1

        check2 = dt.Rows(a)(0).ToString()

        Dim mychart As Chart = New Chart
        Dim ChartArea1 As ChartArea = New ChartArea
        Dim Legend1 As Legend = New Legend

        'Dim dt As DataTable = GetData(query) '30 sec to process
        Dim x As String() = New String(dt.Rows.Count - 1) {}
        Dim y As Integer() = New Integer(dt.Rows.Count - 1) {}
        For i As Integer = 0 To dt.Rows.Count - 1

            x(i) = dt.Rows(i)(1).ToString()
            y(i) = Convert.ToInt32(dt.Rows(i)(2))
        Next

        mychart.Width = 600
        mychart.Height = 400

        mychart.ChartAreas.Clear()
        mychart.ChartAreas.Add("ChartArea2")

        mychart.Series.Clear()
        mychart.Series.Add(0)
        mychart.Series(0).Points.DataBindXY(x, y)

        mychart.Titles.Clear()
        mychart.Titles.Add("[" & a + 1 & "] " & check2.ToString.ToUpper)
        mychart.Titles(0).Font = New System.Drawing.Font("Tahoma", 12, System.Drawing.FontStyle.Bold)
        mychart.Titles(0).BackColor = Color.PaleTurquoise
        mychart.Titles(0).ForeColor = Color.Black

        mychart.Series(0).ChartType = SeriesChartType.Pie
        mychart.Series(0).LegendText = "#VALX"
        mychart.Series(0)("BarLabelStyle") = "Center"
        mychart.Series(0)("pointWidth") = "1"
        mychart.Series(0).BorderDashStyle = ChartDashStyle.Solid
        mychart.Series(0).BorderWidth = 2
        mychart.Series(0).Label = "#PERCENT"
        mychart.Series(0).ShadowColor = Color.Gray
        mychart.Series(0).ShadowOffset = 10

        mychart.Series(0).LabelBackColor = Drawing.Color.Cornsilk
        mychart.Series(0).Font = New Font("Tahoma", 9, FontStyle.Bold)

        mychart.Series(0).LegendToolTip = "#VALX - #PERCENT"
        mychart.Series(0).ToolTip = "#VALX - #PERCENT"

        mychart.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True

        mychart.Series(0).CustomProperties = "DrawingStyle=LightToDark"
        'new
        Chart1.Series(0).CustomProperties = "PieLabelStyle=Outside"

        mychart.ChartAreas("ChartArea1").BorderDashStyle = BorderStyle.Solid
        mychart.Palette = ChartColorPalette.None
        mychart.Series(0).BorderDashStyle = ChartDashStyle.Solid
        mychart.Series(0).BorderWidth = 2
        mychart.Series(0).BorderColor = Color.Black

        mychart.PaletteCustomColors = {Drawing.Color.Black, Drawing.Color.White, Drawing.Color.Blue, Drawing.Color.Yellow, Drawing.Color.Red, Drawing.Color.Orange, Drawing.Color.Green}

        mychart.Legends.Clear()
        mychart.Legends.Add(0)
        mychart.Legends(0).Font = New Font("Tahoma", 10, FontStyle.Bold)
        mychart.Legends(0).Docking = System.Web.UI.DataVisualization.Charting.Docking.Bottom

        mychart.DataBind()

        If (a + 1) Mod 2 <> 0 Then

            PlaceHolder1.Controls.Add(mychart)

        End If

        If (a + 1) Mod 2 = 0 Then

            PlaceHolder2.Controls.Add(mychart)

        End If

    Next
End Sub

 Private Shared Function GetData(ByVal query As String) As DataTable

    Dim dt As New DataTable()
    Dim cmd As New SqlCommand(query)
    '  Dim constr As [String] = ConfigurationManager.ConnectionStrings("SQLDB_pp").ConnectionString
    'Dim con As New SqlConnection(SQLDB_pp)
    Dim sda As New SqlDataAdapter()
    cmd.CommandType = CommandType.Text
    cmd.Connection = SQLDB_pp
    sda.SelectCommand = cmd
    sda.Fill(dt)
    Return dt

End Function

编辑2:

Dim sb As New StringBuilder()

    sb.Append("SELECT Z.MacID, Z.EventName, ISNULL(DIFF,0) AS DIFF FROM (select distinct MacID, EventName from dbo.tblMachine a " _
            & "join (SELECT DISTINCT EVENTNAME FROM dbo.tblEvtDur where EventName <> 'ON'  ) b on b.EventName <> ''  and MacID in (")

    Dim params As New List(Of SqlParameter)()

    Dim max As Integer = result.Length - 1

    For i As Integer = 0 To max

        If i = max Then
            sb.Append("MacID" & i.ToString())
        Else
            sb.Append("MacID" & i.ToString() & ", ")
        End If
        params.Add(New SqlParameter("MacID" & i.ToString(), result(i)))
    Next

    sb.Append(") ) Z " _
            & "LEFT JOIN (SELECT A.MacID, A.EventName, SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) as diff,round(SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) / cast(aVG(Tdiff) " _
            & "as decimal(30,8)),4) * 100 AS PER FROM dbo.tblEvtDur  A LEFT JOIN ( SELECT MacID, SUM(DATEDIFF(SECOND, STARTdt, eNDdt)) as Tdiff FROM  dbo.tblEvtDur " _
            & "WHERE DayID between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  GROUP BY MacID ) B ON  A.MacID = B.MacID WHERE DayID " _
            & "between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  AND A.MacID in (")

    Dim params2 As New List(Of SqlParameter)()

    Dim max2 As Integer = result.Length - 1

    For j As Integer = 0 To max2

        If j = max2 Then
            sb.Append("MacID" & j.ToString())
        Else
            sb.Append("MacID" & j.ToString() & ", ")
        End If
        params2.Add(New SqlParameter("MacID" & j.ToString(), result(j)))
    Next

    sb.Append(") group by A.MacID, A.EventName) a " _
            & "ON A.EVENTNAME = Z.EVENTNAME  and Z.MacID  = a.MacID order by Z.MacID, Z.EventName")

    Dim q As String = sb.ToString()

编辑4:

这是我遇到问题的潜水艇的完整代码。这是原始代码,在第二次SQL加载时间较慢的情况下进行任何修改。

Sub draw_chart1() 'All Machines

    Dim check1, check2, fi, cnt

    Dim sql
    Dim myConnectionString As String = "Provider=SQLOLEDB;" & SQLDB_pp.ConnectionString
    fi = 0
    cnt = 0

    PlaceHolder1.Dispose()
    PlaceHolder2.Dispose()
    'Get all machines
    Dim query1 As String = String.Format("SELECT DISTINCT A.MacID FROM dbo.tblMachine A LEFT JOIN  dbo.tblDataHdr b on a.MacID = b.MacID where DayID between '" & TextBox1.Text & "' AND '" & TextBox3.Text & "' ORDER BY A.MACID")
    Dim dt1 As DataTable = GetData(query1)


    'Dim query As String = String.Format("SELECT Z.MacID, Z.EventName, ISNULL(DIFF,0) AS DIFF FROM (select distinct MacID, EventName from dbo.tblMachine a " _
    '                                        & "join (SELECT DISTINCT EVENTNAME FROM dbo.tblEvtDur where EventName <> 'ON'  ) b on b.EventName <> ''  and MacID in ('" & check1 & "') ) Z " _
    '                                        & "LEFT JOIN (SELECT A.MacID, A.EventName, SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) as diff,round(SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) / cast(aVG(Tdiff) " _
    '                                        & "as decimal(30,8)),4) * 100 AS PER FROM dbo.tblEvtDur  A LEFT JOIN ( SELECT MacID, SUM(DATEDIFF(SECOND, STARTdt, eNDdt)) as Tdiff FROM  dbo.tblEvtDur " _
    '                                        & "WHERE DayID between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  GROUP BY MacID ) B ON  A.MacID = B.MacID WHERE DayID " _
    '                                        & "between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  AND A.MacID in ('" & check1 & "') group by A.MacID, A.EventName) a " _
    '                                        & "ON A.EVENTNAME = Z.EVENTNAME  and Z.MacID  = a.MacID order by Z.MacID, Z.EventName")
    'Dim dt As DataTable = GetData(query)

    For q As Integer = 0 To dt1.Rows.Count - 1

        check1 = dt1.Rows(q)(0).ToString()

        Dim query As String = String.Format("SELECT Z.MacID, Z.EventName, ISNULL(DIFF,0) AS DIFF FROM (select distinct MacID, EventName from dbo.tblMachine a " _
                                            & "join (SELECT DISTINCT EVENTNAME FROM dbo.tblEvtDur where EventName <> 'ON'  ) b on b.EventName <> ''  and MacID in ('" & check1 & "') ) Z " _
                                            & "LEFT JOIN (SELECT A.MacID, A.EventName, SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) as diff,round(SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) / cast(aVG(Tdiff) " _
                                            & "as decimal(30,8)),4) * 100 AS PER FROM dbo.tblEvtDur  A LEFT JOIN ( SELECT MacID, SUM(DATEDIFF(SECOND, STARTdt, eNDdt)) as Tdiff FROM  dbo.tblEvtDur " _
                                            & "WHERE DayID between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  GROUP BY MacID ) B ON  A.MacID = B.MacID WHERE DayID " _
                                            & "between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  AND A.MacID in ('" & check1 & "') group by A.MacID, A.EventName) a " _
                                            & "ON A.EVENTNAME = Z.EVENTNAME  and Z.MacID  = a.MacID order by Z.MacID, Z.EventName")

        Dim dt As DataTable = GetData(query) '47 sec to process



        Dim mychart As Chart = New Chart
        Dim ChartArea1 As ChartArea = New ChartArea
        Dim Legend1 As Legend = New Legend

        'Dim dt As DataTable = GetData(query) '30 sec to process
        Dim x As String() = New String(dt.Rows.Count - 1) {}
        Dim y As Integer() = New Integer(dt.Rows.Count - 1) {}
        For i As Integer = 0 To dt.Rows.Count - 1

            x(i) = dt.Rows(i)(1).ToString()
            ' y(i) = dt.Rows(i)(2).ToString()
            y(i) = Convert.ToInt32(dt.Rows(i)(2))
        Next

        'Dim myConnection As New OleDbConnection(myConnectionString)
        'Dim myCommand As New OleDbCommand(sql, myConnection)
        'mychart.Width = Unit.Pixel(Session("sw") - 100)
        'mychart.Height = Unit.Pixel((Session("sh") / 2) - 88)

        mychart.Width = 600
        mychart.Height = 400

        mychart.ChartAreas.Clear()
        mychart.ChartAreas.Add("ChartArea1")

        mychart.Series.Clear()
        mychart.Series.Add(0)
        mychart.Series(0).Points.DataBindXY(x, y)

        mychart.Titles.Clear()
        mychart.Titles.Add("[" & q + 1 & "] " & check1.ToString.ToUpper)
        mychart.Titles(0).Font = New System.Drawing.Font("Tahoma", 12, System.Drawing.FontStyle.Bold)
        mychart.Titles(0).BackColor = Color.PaleTurquoise
        mychart.Titles(0).ForeColor = Color.Black

        mychart.Series(0).ChartType = SeriesChartType.Pie
        '  mychart.Series(0).Points.DataBindXY(x, y)
        mychart.Series(0).LegendText = "#VALX"
        mychart.Series(0)("BarLabelStyle") = "Center"
        mychart.Series(0)("pointWidth") = "1"
        mychart.Series(0).BorderDashStyle = ChartDashStyle.Solid
        mychart.Series(0).BorderWidth = 2
        mychart.Series(0).Label = "#PERCENT"
        mychart.Series(0).ShadowColor = Color.Gray
        mychart.Series(0).ShadowOffset = 10

        mychart.Series(0).LabelBackColor = Drawing.Color.Cornsilk
        mychart.Series(0).Font = New Font("Tahoma", 9, FontStyle.Bold)

        'Chart1.Series(0).LabelToolTip = "#LABEL Percent: #PERCENT"
        mychart.Series(0).LegendToolTip = "#VALX - #PERCENT"
        mychart.Series(0).ToolTip = "#VALX - #PERCENT"

        mychart.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True

        mychart.Series(0).CustomProperties = "DrawingStyle=LightToDark"
        'new
        Chart1.Series(0).CustomProperties = "PieLabelStyle=Outside"

        mychart.ChartAreas("ChartArea1").BorderDashStyle = BorderStyle.Solid
        mychart.Palette = ChartColorPalette.None
        mychart.Series(0).BorderDashStyle = ChartDashStyle.Solid
        mychart.Series(0).BorderWidth = 2
        mychart.Series(0).BorderColor = Color.Black

        mychart.PaletteCustomColors = {Drawing.Color.Black, Drawing.Color.White, Drawing.Color.Blue, Drawing.Color.Yellow, Drawing.Color.Red, Drawing.Color.Orange, Drawing.Color.Green}

        mychart.Legends.Clear()
        mychart.Legends.Add(0)
        'Chart1.Legends(0).Enabled = True
        ''Chart1.Legends(0).BackColor = Drawing.Color.LightGreenplace
        mychart.Legends(0).Font = New Font("Tahoma", 10, FontStyle.Bold)
        mychart.Legends(0).Docking = System.Web.UI.DataVisualization.Charting.Docking.Bottom
        'Chart1.Legends(0).Alignment = Drawing.StringAlignment.Center
        'Chart1.Legends(0).BackColor = System.Drawing.Color.Transparent

        mychart.DataBind()

        'myplace.Visible = True
        If (q + 1) Mod 2 <> 0 Then
            PlaceHolder1.Controls.Add(mychart)
            ' Dim spacer As LiteralControl = New LiteralControl("<p />")
            ' PlaceHolder1.Controls.Add(spacer)
        End If
        'Exit For
        If (q + 1) Mod 2 = 0 Then

            PlaceHolder2.Controls.Add(mychart)
            'Dim spacer As LiteralControl = New LiteralControl("<p />")
            ' PlaceHolder2.Controls.Add(spacer)

        End If

    Next
End Sub

3 个答案:

答案 0 :(得分:1)

您可以使用String.Join()来连接所有DataTable列值,方法是先将DataTable列转换为字符串数组:

Dim dt1 As DataTable = GetData(query1)

Dim arr As String() = dt1.AsEnumerable().[Select](Function(x) x.Field(Of String)("MacId")).ToArray()

Dim result As String = String.Join(",", arr)

或使用此行:

Dim result As String = table.AsEnumerable().[Select](Function(x) x("MacId").ToString()).Aggregate(Function(a, b) String.Concat(a, "," & b))

之后,可以将输出字符串传递给具有表值参数的查询字符串,或者使用StringBuilderToArray()通过WHERE IN查询创建的字符串数组的每个元素添加参数:

Dim sb As New StringBuilder()

' example query string
sb.Append("SELECT * FROM TableName WHERE ColumnName IN (")

Dim cmd As New SqlCommand()

' note: array is zero-based
Dim max As Integer = arr.Length - 1

For i As Integer = 0 To max
    If i = max Then
       sb.Append("@MacId" & i.ToString())
    Else
       sb.Append("@MacId" & i.ToString() & ",")
    End If
    cmd.Parameters.AddWithValue("@MacId" & i.ToString(), arr(i))
Next

sb.Append(")")

Dim query As String = sb.ToString()

cmd.CommandText = query

' execute the query

更新

由于查询是在函数内部执行的,因此需要将查询参数传递给函数:

Dim sb As New StringBuilder()

' example query string
sb.Append("SELECT * FROM TableName WHERE ColumnName IN (")

Dim params As New List(Of SqlParameter)()

For i As Integer = 0 To max
    If i = max Then
       sb.Append("@MacId" & i.ToString())
    Else
       sb.Append("@MacId" & i.ToString() & ",")
    End If
    params.Add(New SqlParameter("@MacId" & i.ToString(), arr(i)))
Next

sb.Append(")")

Dim query As String = sb.ToString()

dt = GetData(query, params)

功能内容

Private Shared Function GetData(ByVal query As String, Optional ByVal parameters As List(Of SqlParameter) = Nothing) As DataTable

    Dim dt As New DataTable()
    Dim cmd As New SqlCommand(query)

    If parameter IsNot Nothing Then
       cmd.Parameters.AddRange(parameters.ToArray())
    End If

    Dim sda As New SqlDataAdapter()
    cmd.CommandType = CommandType.Text
    cmd.Connection = SQLDB_pp
    sda.SelectCommand = cmd
    sda.Fill(dt)
    Return dt

End Function

答案 1 :(得分:0)

您可以使用string.Join来对特定列的所有Rows值进行修补,并使用以下分隔符:", "(逗号+空格)。
< / p>

使用DataTable.RowsDataRowCollection)作为源,使用LINQ Select()方法,可以将Rows[Column]内容转换为所需的字符串格式。

可能的结果:

Dim separator As String = ", "
Dim ColumnIndex as Integer = 0

Dim AllColumnValues As String = String.Join(separator, 
    dt1.Rows.OfType(Of DataRow).Select(Function(r) "'" + r(ColumnIndex).ToString() + "'"))


如果要引用System.Data.DataSetExtension程序集,则可以使用DataTable.AsEnumerable扩展名。代码可能会像这样更改:

Dim AllColumnValues As String = String.Join(separator,
    dt1.AsEnumerable.Select(Function(r) "'" + r(ColumnIndex).ToString() + "'"))

如果某些值可以为null / empty,则可能需要添加一个Where()子句,以过滤null值:

Dim AllColumnValues As String = String.Join(separator, 
    dt1.AsEnumerable.Where(Function(r) Not r.IsNull(ColumnIndex)).
        Select(Function(r) "'" + r(ColumnIndex).ToString() + "'"))

答案 2 :(得分:0)

最后,经过数小时的反复试验,我终于使字符串正常工作了。

自我说明:拥有GridView来检查DataTable内容是一个巨大的帮助。

现在我的代码基本上看起来像这样:

    Dim query1 As String = String.Format("SELECT DISTINCT A.MacID FROM dbo.tblMachine A LEFT JOIN  dbo.tblDataHdr b on a.MacID = b.MacID where DayID between '" & TextBox1.Text & "' AND '" & TextBox3.Text & "' ORDER BY A.MACID")
    Dim dt1 As DataTable = GetData(query1)


    Dim result As String = dt1.AsEnumerable().[Select](Function(x) x("MacId").ToString()).Aggregate(Function(a, b) String.Concat(a & "'" & "," & "'" & b))


    Dim query As String = String.Format("SELECT Z.MacID, Z.EventName, ISNULL(DIFF,0) AS DIFF FROM (select distinct MacID, EventName from dbo.tblMachine a " _
                                            & "join (SELECT DISTINCT EVENTNAME FROM dbo.tblEvtDur where EventName <> 'ON'  ) b on b.EventName <> ''  and MacID in ('" & result & "') ) Z " _
                                            & "LEFT JOIN (SELECT A.MacID, A.EventName, SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) as diff,round(SUM(DATEDIFF(SECOND, A.STARTdt, A.eNDdt)) / cast(aVG(Tdiff) " _
                                            & "as decimal(30,8)),4) * 100 AS PER FROM dbo.tblEvtDur  A LEFT JOIN ( SELECT MacID, SUM(DATEDIFF(SECOND, STARTdt, eNDdt)) as Tdiff FROM  dbo.tblEvtDur " _
                                            & "WHERE DayID between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  GROUP BY MacID ) B ON  A.MacID = B.MacID WHERE DayID " _
                                            & "between '" & TextBox1.Text & "' and '" & TextBox3.Text & "'  AND A.MacID in ('" & result & "') group by A.MacID, A.EventName) a " _
                                            & "ON A.EVENTNAME = Z.EVENTNAME  and Z.MacID  = a.MacID order by Z.MacID, Z.EventName")
    Dim dt As DataTable = GetData(query)

'Use this to help check the DataTable/Strings
GridView2.DataSource = dt 'change what you want to check accordingly
GridView2.DataBind()

通过使用GridView并将数据绑定到每个查询,确实可以帮助我了解数据的外观。 dt中显示的数据看起来很完美。

现在,我的下一个问题实际上是使用数据为每台计算机创建一个饼图。但我会在另一个问题中发帖。