按降序排序数据库记录

时间:2015-02-26 16:53:37

标签: database vb.net sorting listbox ado

我相对较新的编程和VB并希望得到一些帮助。我试图按照速度(最快速度)的顺序列出跑步者,数据来自数据库,我能够按升序对数据进行排序,但不能按降序排列,有没有办法简单地做到这一点,如果不是最简单的方法来反转列表框。

我的代码:

Private Sub RunningRanking()
    Dim DBconn As New ADODB.Connection 'This is a connection string   declaration'
    Dim Record As New ADODB.Recordset ' This is a connection to the record
    DBconn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _
               "Data source = '" & Application.StartupPath & "\Users Database.mdb'") 'this is opening the connection between the 
    'system and database
    With Record
        .CursorLocation = ADODB.CursorLocationEnum.adUseClient
        .Open("Trainee", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
        .Sort = "RunningSpeed" 'sorting the database record in ascending order
        Do Until Record.EOF
            LstBoxName.Items.Add(Record.Fields("First Name").Value)
            LstBoxSpeed.Items.Add(Record.Fields("RunningSpeed").Value)
            .MoveNext()
        Loop
    End With
    Record.Close()
    DBconn.Close()
End Sub

1 个答案:

答案 0 :(得分:2)

您可以使用desc关键字对数据降序进行排序:

.Sort = "RunningSpeed desc"

考虑使用SQL查询来获取数据而不是打开表。数据库排序通常比Recordset对象快得多。