Access Database VB - 搜索数据库中的大多数'最近'记录

时间:2017-05-14 13:34:08

标签: sql database vb.net visual-studio

我想知道如何更改下面的代码,允许我搜索最新的记录。我正在创建一个酒店预订系统,想要使用数据库中的最新价格,但此刻,它只是使用我不想要的标签进行搜索。

Dim str1 As String
Dim dbpassword As String = "123"
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= E:\Computing\Hotel Booking System\Database\Hotel Booking System.accdb ;Jet OLEDB:Database Password =" & dbpassword & ";"
Dim MyConn As OleDbConnection
Dim dr As OleDbDataReader

Private Sub Information_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim PriceFound As String = False
    MyConn = New OleDbConnection

    MyConn.ConnectionString = connString

    MyConn.Open()

    str1 = ("SELECT * FROM [Prices] WHERE [Adult] = '" & LblPriceAdult.Text & "' AND [Child] = '" & LblPriceChild.Text & "'")

    Dim cmd1 As OleDbCommand = New OleDbCommand(str1, MyConn)

    dr = cmd1.ExecuteReader

    While dr.Read()


        PriceFound = True
        DateDisplay = dr("ID").ToString
        AdultPrice = dr("Adult").ToString
        ChildPrice = dr("Child").ToString
        SingleRoom = dr("Single").ToString
        DoubleRoom = dr("Double").ToString
        FamilyRoom = dr("Family").ToString

        If PriceFound = True Then
            LblPriceAdult.Text = AdultPrice
            LblPriceChild.Text = ChildPrice
            LblPriceDoubleRoom.Text = DoubleRoom
            LblPriceFamilyRoom.Text = FamilyRoom
            LblPriceSingleRoom.Text = SingleRoom
        End If
    End While
    MyConn.Close()
End Sub

2 个答案:

答案 0 :(得分:2)

根据您之前的评论,您需要重写SQL以捕获最新记录。

尝试这样的事情:

 SELECT MAX(ID) FROM [Prices] ORDER BY ID DESC

答案 1 :(得分:0)

I tried the answer above. However for the code above, it wouldn't search for the most recent so I changed DESC to ASC