VB集合,数组,迭代和Queires

时间:2013-12-18 21:21:21

标签: sql arrays vb.net collections iteration

有人可以帮我改进下面的代码吗?我只知道一旦我看到迭代,数组和集合如何正确地用于下面的代码即时编写的情况,我将能够编写更高效的程序。

 Private Sub GetStackRanking()
    Dim SortOrder As String = lblSortOrder.Text


    If lblShowRouteDelivery.Text = "ROUTES" Then

        If lblShowYTDWeekly.Text = "YTD" Then
            'STACK 1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            Dim StackRank1 As String = "SELECT SELECT(EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & ""
            Dim StackRank1Conn As New SqlCommand(StackRank1, MySQLConnection)
            'OPEN CONNECTION
            If MySQLConnection.State = ConnectionState.Closed Then
                MySQLConnection.Open()
            End If
            'READ DATA FROM CONNECTION AND DISPLAY IT
            Dim StackRank1Reader As SqlDataReader = StackRank1Conn.ExecuteReader()
            If StackRank1Reader.HasRows Then
                While StackRank1Reader.Read
                    LblStckRnk1.Text = CType(StackRank1Reader.Item(0), String)
                End While
            Else
                LblStckRnk1.Text = "---"
            End If
            'CLOSE CONNECTIONS
            StackRank1Reader.Close()

            'STACK 2\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            Dim StackRank2 As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & ""
            Dim StackRank2Conn As New SqlCommand(StackRank2, MySQLConnection)

            'READ DATA FROM CONNECTION AND DISPLAY IT
            Dim StackRank2Reader As SqlDataReader = StackRank2Conn.ExecuteReader()
            If StackRank2Reader.HasRows Then
                While StackRank2Reader.Read
                    LblStckRnk2.Text = CType(StackRank2Reader.Item(0), String)
                End While
            Else
                LblStckRnk2.Text = "---"
            End If
            'CLOSE CONNECTIONS
            StackRank2Reader.Close()

            'STACK 3\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            Dim StackRank3 As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk2.Text & "' GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & ""
            Dim StackRank3Conn As New SqlCommand(StackRank3, MySQLConnection)

            'READ DATA FROM CONNECTION AND DISPLAY IT
            Dim StackRank3Reader As SqlDataReader = StackRank3Conn.ExecuteReader()
            If StackRank3Reader.HasRows Then
                While StackRank3Reader.Read
                    LblStckRnk3.Text = CType(StackRank3Reader.Item(0), String)
                End While
            Else
                LblStckRnk3.Text = "---"
            End If
            'CLOSE CONNECTIONS
            StackRank3Reader.Close()

            'STACK 4\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            Dim StackRank4 As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk2.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk3.Text & "' GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & ""
            Dim StackRank4Conn As New SqlCommand(StackRank4, MySQLConnection)

            'READ DATA FROM CONNECTION AND DISPLAY IT
            Dim StackRank4Reader As SqlDataReader = StackRank4Conn.ExecuteReader()
            If StackRank4Reader.HasRows Then
                While StackRank4Reader.Read
                    LblStckRnk4.Text = CType(StackRank4Reader.Item(0), String)
                End While
            Else
                LblStckRnk4.Text = "---"
            End If
            'CLOSE CONNECTIONS
            StackRank4Reader.Close()

            'STACK 5\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            Dim StackRank5 As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk2.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk3.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk4.Text & "' GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & ""
            Dim StackRank5Conn As New SqlCommand(StackRank5, MySQLConnection)

            'READ DATA FROM CONNECTION AND DISPLAY IT
            Dim StackRank5Reader As SqlDataReader = StackRank5Conn.ExecuteReader()
            If StackRank5Reader.HasRows Then
                While StackRank5Reader.Read
                    LblStckRnk5.Text = CType(StackRank5Reader.Item(0), String)
                End While
            Else
                LblStckRnk5.Text = "---"
            End If
            'CLOSE CONNECTIONS
            StackRank5Reader.Close()

            'STACK 6\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            Dim StackRank6 As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk2.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk3.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk4.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk5.Text & "' GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & ""
            Dim StackRank6Conn As New SqlCommand(StackRank6, MySQLConnection)

            'READ DATA FROM CONNECTION AND DISPLAY IT
            Dim StackRank6Reader As SqlDataReader = StackRank6Conn.ExecuteReader()
            If StackRank6Reader.HasRows Then
                While StackRank6Reader.Read
                    LblStckRnk6.Text = CType(StackRank6Reader.Item(0), String)
                End While
            Else
                LblStckRnk6.Text = "---"
            End If
            'CLOSE CONNECTIONS
            StackRank6Reader.Close()

            'STACK 7\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            Dim StackRank7 As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk2.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk3.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk4.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk5.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk6.Text & "' GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & ""
            Dim StackRank7Conn As New SqlCommand(StackRank7, MySQLConnection)

            'READ DATA FROM CONNECTION AND DISPLAY IT
            Dim StackRank7Reader As SqlDataReader = StackRank7Conn.ExecuteReader()
            If StackRank7Reader.HasRows Then
                While StackRank7Reader.Read
                    LblStckRnk7.Text = CType(StackRank7Reader.Item(0), String)
                End While
            Else
                LblStckRnk7.Text = "---"
            End If
            'CLOSE CONNECTIONS
            StackRank7Reader.Close()

            'STACK 8\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            Dim StackRank8 As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk2.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk3.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk4.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk5.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk6.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk7.Text & "' GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & ""
            Dim StackRank8Conn As New SqlCommand(StackRank8, MySQLConnection)

            'READ DATA FROM CONNECTION AND DISPLAY IT
            Dim StackRank8Reader As SqlDataReader = StackRank8Conn.ExecuteReader()
            If StackRank8Reader.HasRows Then
                While StackRank8Reader.Read
                    LblStckRnk8.Text = CType(StackRank8Reader.Item(0), String)
                End While
            Else
                LblStckRnk8.Text = "---"
            End If
            'CLOSE CONNECTIONS
            StackRank8Reader.Close()

            'STACK 9\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            Dim StackRank9 As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk2.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk3.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk4.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk5.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk6.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk7.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk8.Text & "' GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & ""
            Dim StackRank9Conn As New SqlCommand(StackRank9, MySQLConnection)

            'READ DATA FROM CONNECTION AND DISPLAY IT
            Dim StackRank9Reader As SqlDataReader = StackRank9Conn.ExecuteReader()
            If StackRank9Reader.HasRows Then
                While StackRank9Reader.Read
                    LblStckRnk9.Text = CType(StackRank9Reader.Item(0), String)
                End While
            Else
                LblStckRnk9.Text = "---"
            End If
            'CLOSE CONNECTIONS
            StackRank9Reader.Close()

            'STACK 10\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            Dim StackRank10 As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = " & lblAreaOBJID.Text & " AND EMPLOYEE_NAME <> '" & LblStckRnk1.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk2.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk3.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk4.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk5.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk6.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk7.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk8.Text & "' AND EMPLOYEE_NAME <> '" & LblStckRnk9.Text & "' GROUP BY EMPLOYEE_NAME ORDER BY YIELD " & lblSortOrder.Text & ""
            Dim StackRank10Conn As New SqlCommand(StackRank10, MySQLConnection)

            'READ DATA FROM CONNECTION AND DISPLAY IT
            Dim StackRank10Reader As SqlDataReader = StackRank10Conn.ExecuteReader()
            If StackRank10Reader.HasRows Then
                While StackRank10Reader.Read
                    LblStckRnk10.Text = CType(StackRank10Reader.Item(0), String)
                End While
            Else
                LblStckRnk10.Text = "---"
            End If
            'CLOSE CONNECTIONS
            StackRank10Reader.Close()

1 个答案:

答案 0 :(得分:1)

该代码有几件事情只是以令人困惑的方式破解(例如,将MySql与Sql Server ado.net提供程序对象混合)。我现在希望为你做的最好的事情就是给你一个看起来很正确的例子。

Private Function GetStackRanking1(ByVal AreaObjectID As Integer) As String

    Dim sql As String = "SELECT EMPLOYEE_NAME, AVG(EFFICIENCY_YIELD) AS YIELD FROM dbo.APE_BUSDRIVER_MAIN WHERE APE_AREA_OBJID = @AreaObjectId GROUP BY EMPLOYEE_NAME"

    Using cn As New SqlConnection("connection string here"), _
          cmd As New SqlCommand(sql, cn)

        cmd.Parameters.Add("@AreaObjectId", SqlDbType.Integer).Value = AreaObjectId
        cn.Open()
        Return CStr(cmd.ExecuteScalar())
   End Using
End Function

你会这样称呼它:

LblStckRnk1.Text = GetStackRanking1(Integer.Parse(lblAreaOBJID.Text))

请注意,用户界面对象不是调用数据库的方法的任何位置。另请注意Using块。这可以确保连接将被关闭,即使抛出异常也 。最重要的是,请注意区域对象ID如何发送到查询。这样可以防止sql注入的任何可能性。