下面的代码做了一些事情:
通过我在Windows窗体上的标签集合进行迭代,用作占位符
在遍历集合时,代码连接到SQL数据库以返回每个标签的查询结果(大约104个标签)
最后一些图表填充。
我已经计划了整个过程,大约需要4到5秒。我的目标是立即执行此过程或不超过1秒。
那位专家可以向我解释一下我做错了什么以及为什么这个过程需要这么长时间?
这是查询:
Dim RESULT1 As Decimal 'declare this as global
Dim RESULT2 As Decimal 'declare this as global
Private Sub Week(ByVal week As Integer)
Dim queryString As String = "SELECT " & _
" (SELECT CAST(SUM(TARGET_SECONDS) AS DECIMAL)/ CAST(SUM(ROUTE_SECONDS) AS DECIMAL) FROM dbo.APE_BUSDRIVER_MAIN WITH(NOLOCK) WHERE ACTIVE = 1 AND EMPLOYEE_NAME = '" & cbEmployeeName.Text & "') AS RESULT1," & _
" (SELECT (SELECT CAST(COUNT(APE_BUSDRIVER_STATUS_OBJID) AS DECIMAL) FROM dbo.APE_BUSDRIVER_MAIN AS RESULT2 WHERE ACTIVE = 1 AND APE_BUSDRIVER_STATUS_OBJID= 1 AND EMPLOYEE_NAME = '" & cbEmployeeName.Text & "' )/(SELECT CAST(COUNT(APE_BUSDRIVER_STATUS_OBJID) AS DECIMAL) FROM dbo.APE_BUSDRIVER_MAIN AS RESULT2 WHERE ACTIVE = 1 AND EMPLOYEE_NAME = '" & cbEmployeeName.Text & "' )) AS RESULT2" & _
" FROM dbo.APE_BUSDRIVER_MAIN WHERE WEEK_TIME = " & week & " AND APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME = '" & cbEmployeeName.Text & "' AND YEAR_TIME = '" & cbYear.Text & "' 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.
If reader.HasRows Then
While reader.Read()
RESULT1 = reader("RESULT1")
RESULT2 = reader("RESULT2")
End While
Else
RESULT1 = 0
RESULT2 = 0
End If
' Call Close when done reading.
reader.Close()
End Using
End Sub
以下是代码:
Private Sub LoadWeeklyStats()
For i As Integer = 0 To 51
Dim LabelWkEff As String = "LblWkEff" + (i + 1).ToString
Dim myArray1 As Array = Controls.Find(LabelWkEff, False)
Dim myControl1 As Label = myArray1(0)
myControl1.Text = RESULT1
'AND
Dim LabelDeliveryStat As String = "lblDeliveryStat" + (i + 1).ToString
Dim myArray2 As Array = Controls.Find(LabelDeliveryStat, False)
Dim myControl2 As Label = myArray2(0)
myControl2.Text = RESULT2
'COUNTER
Week(i + 1)
Next
'TO CLEAR CHART AND RELOAD IT
Chart1.Titles.Clear()
Chart1.Series.Clear()
Chart1.ChartAreas.Clear()
Chart1.ChartAreas.Add("AREA")
Chart1.Series.Add("WeeklyEfficiency")
Chart1.Series.Add("TARGET")
Chart1.Titles.Add("ROUTE EFFICIENCY")
Chart1.Series("TARGET").ChartType = SeriesChartType.Line
With Chart1.Titles(0)
.Font = New Font("TAHOMA", 12, FontStyle.Bold)
End With
With Chart1.ChartAreas(0)
'.AxisY.MajorGrid.Enabled = False
.AxisX.MajorGrid.Enabled = False
.AxisX.Title = "WEEKS"
.AxisY.Title = "EFFICIENCY"
.AxisX.Minimum = 0
.AxisX.Maximum = 53
.AxisY.Minimum = 0
.AxisX.Interval = 1
.AxisY.LabelStyle.Format = Format(0, "0%")
End With
With Chart1.Series(0)
.Font = New Font(Me.Font.Name, 7, FontStyle.Regular)
.SmartLabelStyle.Enabled = False
.LabelAngle = -90
.Label = "#VAL{P}"
.IsValueShownAsLabel = True
.Name = "WeeklyEfficiency"
.ChartType = SeriesChartType.Column
.LabelToolTip = Enabled
.ToolTip = "WEEK #VALX" & vbNewLine & "#VAL{P}"
.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
With .Points
.AddXY(0, 0)
.AddXY(1, LblWkEff1.Text)
.AddXY(2, LblWkEff2.Text)
.AddXY(3, LblWkEff3.Text)
.AddXY(4, LblWkEff4.Text)
.AddXY(5, LblWkEff5.Text)
.AddXY(6, LblWkEff6.Text)
.AddXY(7, LblWkEff7.Text)
.AddXY(8, LblWkEff8.Text)
.AddXY(9, LblWkEff9.Text)
.AddXY(10, LblWkEff10.Text)
.AddXY(11, LblWkEff11.Text)
.AddXY(12, LblWkEff12.Text)
.AddXY(13, LblWkEff13.Text)
.AddXY(14, LblWkEff14.Text)
.AddXY(15, LblWkEff15.Text)
.AddXY(16, LblWkEff16.Text)
.AddXY(17, LblWkEff17.Text)
.AddXY(18, LblWkEff18.Text)
.AddXY(19, LblWkEff19.Text)
.AddXY(20, LblWkEff20.Text)
.AddXY(21, LblWkEff21.Text)
.AddXY(22, LblWkEff22.Text)
.AddXY(23, LblWkEff23.Text)
.AddXY(24, LblWkEff24.Text)
.AddXY(25, LblWkEff25.Text)
.AddXY(26, LblWkEff26.Text)
.AddXY(27, LblWkEff27.Text)
.AddXY(28, LblWkEff28.Text)
.AddXY(29, LblWkEff29.Text)
.AddXY(30, LblWkEff30.Text)
.AddXY(31, LblWkEff33.Text)
.AddXY(32, LblWkEff32.Text)
.AddXY(33, LblWkEff33.Text)
.AddXY(34, LblWkEff34.Text)
.AddXY(35, LblWkEff35.Text)
.AddXY(36, LblWkEff36.Text)
.AddXY(37, LblWkEff37.Text)
.AddXY(38, LblWkEff38.Text)
.AddXY(39, LblWkEff39.Text)
.AddXY(40, LblWkEff40.Text)
.AddXY(41, LblWkEff41.Text)
.AddXY(42, LblWkEff42.Text)
.AddXY(43, LblWkEff43.Text)
.AddXY(44, LblWkEff44.Text)
.AddXY(45, LblWkEff45.Text)
.AddXY(46, LblWkEff46.Text)
.AddXY(47, LblWkEff47.Text)
.AddXY(48, LblWkEff48.Text)
.AddXY(49, LblWkEff49.Text)
.AddXY(50, LblWkEff50.Text)
.AddXY(51, LblWkEff51.Text)
.AddXY(52, LblWkEff52.Text)
.AddXY(53, 0)
End With
End With
With Chart1.Series("TARGET")
.Name = "TARGET"
.ChartType = SeriesChartType.Line
.Color = Color.Red
.BorderWidth = 3
With .Points
.AddXY(0, 1)
.AddXY(1, 1)
.AddXY(2, 1)
.AddXY(3, 1)
.AddXY(4, 1)
.AddXY(5, 1)
.AddXY(6, 1)
.AddXY(7, 1)
.AddXY(8, 1)
.AddXY(9, 1)
.AddXY(10, 1)
.AddXY(11, 1)
.AddXY(12, 1)
.AddXY(13, 1)
.AddXY(14, 1)
.AddXY(15, 1)
.AddXY(16, 1)
.AddXY(17, 1)
.AddXY(18, 1)
.AddXY(19, 1)
.AddXY(20, 1)
.AddXY(21, 1)
.AddXY(22, 1)
.AddXY(23, 1)
.AddXY(24, 1)
.AddXY(25, 1)
.AddXY(26, 1)
.AddXY(27, 1)
.AddXY(28, 1)
.AddXY(29, 1)
.AddXY(30, 1)
.AddXY(31, 1)
.AddXY(32, 1)
.AddXY(33, 1)
.AddXY(34, 1)
.AddXY(35, 1)
.AddXY(36, 1)
.AddXY(37, 1)
.AddXY(38, 1)
.AddXY(39, 1)
.AddXY(40, 1)
.AddXY(41, 1)
.AddXY(42, 1)
.AddXY(43, 1)
.AddXY(44, 1)
.AddXY(45, 1)
.AddXY(46, 1)
.AddXY(47, 1)
.AddXY(48, 1)
.AddXY(49, 1)
.AddXY(50, 1)
.AddXY(51, 1)
.AddXY(52, 1)
.AddXY(53, 1)
End With
End With
'MAKE ONTIME CHART
'______________________________________________________________________________()
'TO CLEAR CHART AND RELOAD IT
Chart2.Titles.Clear()
Chart2.Series.Clear()
Chart2.ChartAreas.Clear()
Chart2.ChartAreas.Add("AREA")
Chart2.Series.Add("WeeklyEfficiency")
Chart2.Series.Add("TARGET")
Chart2.Titles.Add("ON TIME EFFICIENCY")
With Chart2.Titles(0)
.Font = New Font("TAHOMA", 12, FontStyle.Bold)
End With
With Chart2.ChartAreas(0)
'.AxisY.MajorGrid.Enabled = False
.AxisX.MajorGrid.Enabled = False
.AxisX.Title = "WEEKS"
.AxisY.Title = "EFFICIENCY"
.AxisX.Minimum = 0
.AxisX.Maximum = 53
.AxisY.Minimum = 0
.AxisX.Interval = 1
.AxisY.LabelStyle.Format = Format(0, "0%")
End With
With Chart2.Series(0)
.Font = New Font(Me.Font.Name, 7, FontStyle.Regular)
.SmartLabelStyle.Enabled = False
.LabelAngle = -90
.Label = "#VAL{P}"
.IsValueShownAsLabel = True
.Name = "WeeklyEfficiency"
.ChartType = SeriesChartType.Column
.LabelToolTip = Enabled
.ToolTip = "WEEK #VALX" & vbNewLine & "#VAL{P}"
.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(0, 0)
.Points.AddXY(1, lblDeliveryStat1.Text)
.Points.AddXY(2, lblDeliveryStat2.Text)
.Points.AddXY(3, lblDeliveryStat3.Text)
.Points.AddXY(4, lblDeliveryStat4.Text)
.Points.AddXY(5, lblDeliveryStat5.Text)
.Points.AddXY(6, lblDeliveryStat6.Text)
.Points.AddXY(7, lblDeliveryStat7.Text)
.Points.AddXY(8, lblDeliveryStat8.Text)
.Points.AddXY(9, lblDeliveryStat9.Text)
.Points.AddXY(10, lblDeliveryStat10.Text)
.Points.AddXY(11, lblDeliveryStat11.Text)
.Points.AddXY(12, lblDeliveryStat12.Text)
.Points.AddXY(13, lblDeliveryStat13.Text)
.Points.AddXY(14, lblDeliveryStat14.Text)
.Points.AddXY(15, lblDeliveryStat15.Text)
.Points.AddXY(16, lblDeliveryStat16.Text)
.Points.AddXY(17, lblDeliveryStat17.Text)
.Points.AddXY(18, lblDeliveryStat18.Text)
.Points.AddXY(19, lblDeliveryStat19.Text)
.Points.AddXY(20, lblDeliveryStat20.Text)
.Points.AddXY(21, lblDeliveryStat21.Text)
.Points.AddXY(22, lblDeliveryStat22.Text)
.Points.AddXY(23, lblDeliveryStat23.Text)
.Points.AddXY(24, lblDeliveryStat24.Text)
.Points.AddXY(25, lblDeliveryStat25.Text)
.Points.AddXY(26, lblDeliveryStat26.Text)
.Points.AddXY(27, lblDeliveryStat27.Text)
.Points.AddXY(28, lblDeliveryStat28.Text)
.Points.AddXY(29, lblDeliveryStat29.Text)
.Points.AddXY(30, lblDeliveryStat30.Text)
.Points.AddXY(31, lblDeliveryStat33.Text)
.Points.AddXY(32, lblDeliveryStat32.Text)
.Points.AddXY(33, lblDeliveryStat33.Text)
.Points.AddXY(34, lblDeliveryStat34.Text)
.Points.AddXY(35, lblDeliveryStat35.Text)
.Points.AddXY(36, lblDeliveryStat36.Text)
.Points.AddXY(37, lblDeliveryStat37.Text)
.Points.AddXY(38, lblDeliveryStat38.Text)
.Points.AddXY(39, lblDeliveryStat39.Text)
.Points.AddXY(40, lblDeliveryStat40.Text)
.Points.AddXY(41, lblDeliveryStat41.Text)
.Points.AddXY(42, lblDeliveryStat42.Text)
.Points.AddXY(43, lblDeliveryStat43.Text)
.Points.AddXY(44, lblDeliveryStat44.Text)
.Points.AddXY(45, lblDeliveryStat45.Text)
.Points.AddXY(46, lblDeliveryStat46.Text)
.Points.AddXY(47, lblDeliveryStat47.Text)
.Points.AddXY(48, lblDeliveryStat48.Text)
.Points.AddXY(49, lblDeliveryStat49.Text)
.Points.AddXY(50, lblDeliveryStat50.Text)
.Points.AddXY(51, lblDeliveryStat51.Text)
.Points.AddXY(52, lblDeliveryStat52.Text)
.Points.AddXY(53, 0)
End With
With Chart2.Series("TARGET")
.Name = "TARGET"
.ChartType = SeriesChartType.Line
.Color = Color.Red
.BorderWidth = 3
With .Points
.AddXY(0, 1)
.AddXY(1, 1)
.AddXY(2, 1)
.AddXY(3, 1)
.AddXY(4, 1)
.AddXY(5, 1)
.AddXY(6, 1)
.AddXY(7, 1)
.AddXY(8, 1)
.AddXY(9, 1)
.AddXY(10, 1)
.AddXY(11, 1)
.AddXY(12, 1)
.AddXY(13, 1)
.AddXY(14, 1)
.AddXY(15, 1)
.AddXY(16, 1)
.AddXY(17, 1)
.AddXY(18, 1)
.AddXY(19, 1)
.AddXY(20, 1)
.AddXY(21, 1)
.AddXY(22, 1)
.AddXY(23, 1)
.AddXY(24, 1)
.AddXY(25, 1)
.AddXY(26, 1)
.AddXY(27, 1)
.AddXY(28, 1)
.AddXY(29, 1)
.AddXY(30, 1)
.AddXY(31, 1)
.AddXY(32, 1)
.AddXY(33, 1)
.AddXY(34, 1)
.AddXY(35, 1)
.AddXY(36, 1)
.AddXY(37, 1)
.AddXY(38, 1)
.AddXY(39, 1)
.AddXY(40, 1)
.AddXY(41, 1)
.AddXY(42, 1)
.AddXY(43, 1)
.AddXY(44, 1)
.AddXY(45, 1)
.AddXY(46, 1)
.AddXY(47, 1)
.AddXY(48, 1)
.AddXY(49, 1)
.AddXY(50, 1)
.AddXY(51, 1)
.AddXY(52, 1)
.AddXY(53, 1)
End With
End With
End Sub
答案 0 :(得分:0)
我遇到类似的问题我正在开发一个来电显示程序当用户带回来的时候,它已经带回了超过几百条通话记录
我的方法是只在开始时查询数据库一次,然后在数据集上使用linq以恢复各个数据
伊恩
Private Sub LoadWeeklyStats()
'Get Global DataSource Here
For i As Integer = 0 To 51
Dim LabelWkEff As String = "LblWkEff" + (i + 1).ToString
Dim myArray1 As Array = Controls.Find(LabelWkEff, False)
Dim myControl1 As Label = myArray1(0)
myControl1.Text = RESULT1
'AND
Dim LabelDeliveryStat As String = "lblDeliveryStat" + (i + 1).ToString
Dim myArray2 As Array = Controls.Find(LabelDeliveryStat, False)
Dim myControl2 As Label = myArray2(0)
myControl2.Text = RESULT2
'COUNTER
Week(i + 1)
Next