获得数据库中的最高价值

时间:2013-09-28 05:05:39

标签: sql vb.net

    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名候选人,但我只想查看谁拥有最高的累积选票。

1 个答案:

答案 0 :(得分: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)并按投票列对结果进行排序,以便获得最高的投票记录。