简单的For循环对象查询

时间:2013-12-31 16:48:21

标签: vb.net for-loop

以下是更新的问题以及有关我尝试做的更多信息:

Private Sub HistogramYTD(ByVal DP1 As String, ByVal DP2 As String)

    Dim queryString As String = "SELECT COUNT(ROUTE_SECONDS) AS RESULT1 FROM dbo.APE_BUSDRIVER_MAIN WITH(NOLOCK) WHERE YEAR_TIME = '" & cbYear.Text & "' AND APE_AREA_OBJID = '" & lblAreaOBJID.Text & "' AND EMPLOYEE_NAME = '" & cbEmployeeName.Text & "' AND ROUTE_SECONDS >= " & DP1 & " AND ROUTE_SECONDS < " & DP2 & " AND ACTIVE = 1"

    Using connection As New SqlConnection(SQLConnectionStr)
        Dim command As New SqlCommand(queryString, connection)
        connection.Open()

        Dim reader As SqlDataReader = command.ExecuteReader()

        ' Call Read before accessing data. 
        While reader.Read()
            RESULT1 = reader("RESULT1")
        End While

        ' Call Close when done reading.
        reader.Close()
    End Using
End Sub

Private Sub GetHistogram()


    Dim min As Integer = 0
    Dim max As Integer = lblMaxScan.Text
    Dim range As Integer = max / 20

    Dim HistResult1, HistResult2, HistResult3, HistResult4, HistResult5, _
        HistResult6, HistResult7, HistResult8, HistResult9, HistResult10, _
        HistResult11, HistResult12, HistResult13, HistResult14, HistResult15, _
        HistResult16, HistResult17, HistResult18, HistResult19, HistResult20 _
        As Integer

    If lblShowYTDWeekly2.Text = "YTD" Then

        CountRoute()

        HistogramYTD(min, min + (range * 1))
        HistResult1 = RESULT1

        HistogramYTD(min + (range * 1) + 1, min + (range * 2))
        HistResult2 = RESULT1

        HistogramYTD(min + (range * 2) + 1, min + (range * 3))
        HistResult3 = RESULT1

        HistogramYTD(min + (range * 3) + 1, min + (range * 4))
        HistResult4 = RESULT1

        HistogramYTD(min + (range * 4) + 1, min + (range * 5))
        HistResult5 = RESULT1

        HistogramYTD(min + (range * 5) + 1, min + (range * 6))
        HistResult6 = RESULT1

        HistogramYTD(min + (range * 6) + 1, min + (range * 7))
        HistResult7 = RESULT1

        HistogramYTD(min + (range * 7) + 1, min + (range * 8))
        HistResult8 = RESULT1

        HistogramYTD(min + (range * 8) + 1, min + (range * 9))
        HistResult9 = RESULT1

        HistogramYTD(min + (range * 9) + 1, min + (range * 10))
        HistResult10 = RESULT1

        HistogramYTD(min + (range * 10) + 1, min + (range * 11))
        HistResult11 = RESULT1

        HistogramYTD(min + (range * 11) + 1, min + (range * 12))
        HistResult12 = RESULT1

        HistogramYTD(min + (range * 12) + 1, min + (range * 13))
        HistResult13 = RESULT1

        HistogramYTD(min + (range * 13) + 1, min + (range * 14))
        HistResult14 = RESULT1

        HistogramYTD(min + (range * 14) + 1, min + (range * 15))
        HistResult15 = RESULT1

        HistogramYTD(min + (range * 15) + 1, min + (range * 16))
        HistResult16 = RESULT1

        HistogramYTD(min + (range * 16) + 1, min + (range * 17))
        HistResult17 = RESULT1

        HistogramYTD(min + (range * 17) + 1, min + (range * 18))
        HistResult18 = RESULT1

        HistogramYTD(min + (range * 18) + 1, min + (range * 19))
        HistResult19 = RESULT1

        HistogramYTD(min + (range * 19) + 1, min + (range * 20))
        HistResult20 = RESULT1

    End If
-------------------------------------------------------------------------------------

    'TO CLEAR CHART AND RELOAD IT
    Chart3.Titles.Clear()
    Chart3.Series.Clear()
    Chart3.ChartAreas.Clear()
    Chart3.ChartAreas.Add("AREA")
    Chart3.Series.Add("Histogram")
    Chart3.Titles.Add("ROUTE HISTOGRAM")

    With Chart3.Titles(0)
        .Font = New Font("TAHOMA", 12, FontStyle.Bold)
    End With

    With Chart3.ChartAreas(0)
        '.AxisY.MajorGrid.Enabled = False
        .AxisX.MajorGrid.Enabled = False
        .AxisX.Title = "ROUTE TIME"
        .AxisY.Title = "FREQUENCY"
        .AxisX.Minimum = 0
        .AxisX.Maximum = 21
        .AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep30
        .AxisY.Minimum = 0
        .AxisX.Interval = 1
        .AxisX.LabelStyle.Angle = -45
    End With

    With Chart3.Series(0)
        .Font = New Font(Me.Font.Name, 7, FontStyle.Regular)
        .SmartLabelStyle.Enabled = False
        .Label = "#VAL{P}"
        .IsValueShownAsLabel = True
        .Name = "WeeklyEfficiency"
        .ChartType = SeriesChartType.Column
        .LabelToolTip = Enabled
        .ToolTip = "FROM #VALX MINUTES." & vbNewLine & "#VAL ROUTE(S) FOR THIS RANGE."
        .BorderColor = Color.Black
        .LabelForeColor = Color.Transparent
        Dim area As String = cbArea.Text
        Select Case area
            Case "BC_PICKUP"
                .Color = Color.Blue
            Case "BC_DELIVERY"
                .Color = Color.Blue
            Case "RAW"
                .Color = Color.LimeGreen
            Case "RTV"
                .Color = Color.Yellow
            Case Else
                .Color = Color.Black
        End Select

        'LOAD DATA POINTS

        .Points.AddXY(Format(DateAdd("s", min, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 1), "00:00:00"), "mm:ss"), HistResult1)
        .Points.AddXY(Format(DateAdd("s", min + (range * 1) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 2), "00:00:00"), "mm:ss"), HistResult2)
        .Points.AddXY(Format(DateAdd("s", min + (range * 2) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 3), "00:00:00"), "mm:ss"), HistResult3)
        .Points.AddXY(Format(DateAdd("s", min + (range * 3) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 4), "00:00:00"), "mm:ss"), HistResult4)
        .Points.AddXY(Format(DateAdd("s", min + (range * 4) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 5), "00:00:00"), "mm:ss"), HistResult5)
        .Points.AddXY(Format(DateAdd("s", min + (range * 5) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 6), "00:00:00"), "mm:ss"), HistResult6)
        .Points.AddXY(Format(DateAdd("s", min + (range * 6) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 7), "00:00:00"), "mm:ss"), HistResult7)
        .Points.AddXY(Format(DateAdd("s", min + (range * 7) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 8), "00:00:00"), "mm:ss"), HistResult8)
        .Points.AddXY(Format(DateAdd("s", min + (range * 8) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 9), "00:00:00"), "mm:ss"), HistResult9)
        .Points.AddXY(Format(DateAdd("s", min + (range * 9) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 10), "00:00:00"), "mm:ss"), HistResult10)
        .Points.AddXY(Format(DateAdd("s", min + (range * 10) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 11), "00:00:00"), "mm:ss"), HistResult11)
        .Points.AddXY(Format(DateAdd("s", min + (range * 11) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 12), "00:00:00"), "mm:ss"), HistResult12)
        .Points.AddXY(Format(DateAdd("s", min + (range * 12) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 13), "00:00:00"), "mm:ss"), HistResult13)
        .Points.AddXY(Format(DateAdd("s", min + (range * 13) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 14), "00:00:00"), "mm:ss"), HistResult14)
        .Points.AddXY(Format(DateAdd("s", min + (range * 14) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 15), "00:00:00"), "mm:ss"), HistResult15)
        .Points.AddXY(Format(DateAdd("s", min + (range * 15) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 16), "00:00:00"), "mm:ss"), HistResult16)
        .Points.AddXY(Format(DateAdd("s", min + (range * 16) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 17), "00:00:00"), "mm:ss"), HistResult17)
        .Points.AddXY(Format(DateAdd("s", min + (range * 17) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 18), "00:00:00"), "mm:ss"), HistResult18)
        .Points.AddXY(Format(DateAdd("s", min + (range * 18) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 19), "00:00:00"), "mm:ss"), HistResult19)
        .Points.AddXY(Format(DateAdd("s", min + (range * 19) + 1, "00:00:00"), "mm:ss") & " - " & Format(DateAdd("s", min + (range * 20), "00:00:00"), "mm:ss"), HistResult20)
    End With
End Sub

我如何使用类似语法的数组?还是循环?

2 个答案:

答案 0 :(得分:1)

您无法以这种方式动态创建变量并尝试分配值,而是需要使用字典或列表,具体取决于您计划如何访问存储在集合对象中的值

例如,使用词典....

Dim HistResult As New Dictionary(Of String, Single)

For i As Integer = 1 To 19
   HistogramYTD(min + (range * i) + 1, min + (range * (1 + i)))
   HistResult.Add(String.Format("HistResult{0}", i) , RESULT1)
Next

现在你有一个字典,其中每个键都被命名为你的变量“HistResultXX”,你可以使用这样的语法检索正确的值

Dim result = HistResult("HistResult19")

我假设您的变量RESULT1的类型为Single,但如果不是这种情况,则将Dictionary声明更改为正确的数据类型

答案 1 :(得分:0)

我认为你需要的是数组vb.net array

Dim HistResult(19) as Integer

For i As Integer = 0 To  HistResult.GetUpperBound(0) ' HistResult.Length-1

  HistogramYTD(min + (range * i) + 1, min + (range * (1 + i)))

  HistResult(i) = RESULT1

Next

    'plotting inside loop will be the same
    'For i As Integer = 0 To  HistResult.GetUpperBound(0) 
    ' Points.AddXY(Format(DateAdd("s", min, "00:00:00"), "mm:ss") & " - " & 'Format(DateAdd("s", min + (range * i), "00:00:00"), "mm:ss"), HistResult(i))
    ' Next

而不是使用全局RESULT1。 (我假设您从sqldatabase检索一个整数并将其存储到RESULT1)。你应该使用functions并返回结果。所以它将是

HistResult(i) =HistogramYTD(min + (range * i) + 1, min + (range * (1 + i)))

来自你的SQL查询功能。

  1. 而不是ExecuteQuery更好地使用ExecuteScalar

  2. 而不是连接字符串以使命令使用 参数化sql查询

  3. 制作function和参数integer而不是string