0
    Dim elem As String
    elem = "College"
    Dim conn As New SqlClient.SqlConnection
    With conn
        .ConnectionString = ("SERVER=ANINGDZTS-PC;DATABASE=AEVS_2013;Trusted_Connection = yes;")
        .Open()
    End With

    Dim cmd As New SqlClient.SqlCommand
    With cmd
        .CommandText = "SELECT [PositionID],[Position],[Partylist],[Department],[Fullname],[Lvl],[Section],[Votes] FROM tbl_Cand WHERE [Department] = '" & elem & "'"
        .CommandType = CommandType.Text
        .CommandTimeout = 30
        .Connection = conn
    End With
    Dim dt As New DataTable
    dt.Load(cmd.ExecuteReader)

    With DataGridView1
        .AutoGenerateColumns = True
        .DataSource = dt
    End With
    vvv()

    cmd.Dispose()
    cmd = Nothing
    conn.Dispose()
    conn = Nothing

这是我查看所有候选人的代码,但我只想获得累积票数最高的人的姓名和票数。例如,在总统中,我有 2 个候选人,但我只想查看谁的累积票数最高。

4

1 回答 1

0

听起来您只需要修改 sql 语句即可找到最大值。像这样的东西应该工作:

   .CommandText = "SELECT TOP 1 [PositionID],[Position],[Partylist],[Department],[Fullname],[Lvl],[Section],[Votes] FROM tbl_Cand WHERE [Department] = '" & elem & "'" ORDER BY [Votes] DESC

它只选择一条记录 ( TOP 1) 并按投票列对结果进行排序,因此您将获得最高投票记录。

于 2013-09-28T06:12:36.833 回答