2013-09-28 100 views
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個候選人,但我只想看看誰擁有最高的累積選票。獲取數據庫中的最大值

+0

你的問題是什麼? – JohnFx

+0

你只是需要修改SQL –

回答

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),並下令由得票數列的結果,這樣你會得到最高票紀錄。

+0

謝謝!有用! –

相關問題