选择N记录语句不返回正确的金额

时间:2013-12-28 15:20:01

标签: vb.net visual-studio-2010 visual-studio ms-access

我使用sql语句连接到访问数据库并仅检索100条记录。但是,这是返回所有记录而不是100.如何限制我想要获取的记录。如果我在访问中直接作为查询运行此语句,它可以正常工作。

我在vb.Net.Thanks中使用winforms

sql = "Select top 100 * from Requests ORDER BY [Date-time received] DESC"

vb.Net code

    Dim lvcount As Integer = 0
    'Dim tmpColor As Color = Color.Green

    Sub fillRequests()
        Try

            DBConnection.connect()

            sql = "Select top 100 * from Requests ORDER BY [Date-time received] DESC"

            Debug.Print(sql)

            Dim oledbCmd As OleDbCommand = New OleDbCommand(sql, oledbCnn)


            Using dr = oledbCmd.ExecuteReader()

                While dr.Read()
                    If dr.HasRows Then
                        Dim LVI As New ListViewItem

                        With LVI

                            .UseItemStyleForSubItems = False

                            .Text = dr(0).ToString()

                            .SubItems.Add(CDate(dr(5)).ToShortDateString)
                            .SubItems.Add(dr(1).ToString())
                            .SubItems.Add(dr(3).ToString())

                            If dr(3).ToString = "D" Then
                                .SubItems(3).Text = "Destruction"

                            ElseIf dr(3).ToString = "O" Then
                                .SubItems(3).Text = "Retrieval"

                            ElseIf dr(3).ToString = "I" Then
                                .SubItems(3).Text = "Intake"

                            ElseIf dr(3).ToString = "B" Then
                                .SubItems(3).Text = "Return"

                            ElseIf dr(3).ToString = "X" Then
                                .SubItems(3).Text = "Other"

                            ElseIf dr(3).ToString = "1" Then
                                .SubItems(3).Text = "Supply std boxes"

                            ElseIf dr(3).ToString = "G" Then
                                .SubItems(3).Text = "File Return"

                            ElseIf dr(3).ToString = "F" Then
                                .SubItems(3).Text = "File Retrieval"

                            ElseIf dr(3).ToString = "2" Then
                                .SubItems(3).Text = "Supply prt boxes"

                            End If


                            .SubItems.Add(dr(9).ToString())

                            If IsDBNull(dr(9)) Then
                                .SubItems(4).Text = "O/S"
                                .SubItems(4).ForeColor = Color.Red
                                'MessageBox.Show(.SubItems(4).Text)

                            ElseIf dr(9) IsNot "DEMO" Then
                                .SubItems(4).Text = "Done"

                            End If

                        End With
                        lvRequests.Items.Add(LVI)

                        lvcount += 1

                    End If

                End While
            End Using

            'autosize each column in listview

            For Each col As ColumnHeader In lvRequests.Columns
                col.Width = -2
            Next col

            'refresh the list with new data
            'lvRequests.Refresh()

            'Enable the posting previous button
            btnPreviousPostings.Enabled = False
            btnPreviousPostings.Text = "No data to show"
            oledbCnn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            'dr.Close()
            'oledbCnn.Close()
        End Try

        'btnNextPostings.Enabled = ListView1.Items.Count > 100

        If (lvRequests.Items.Count < 100) Then
            btnNextPostings.Enabled = False
            btnNextPostings.Text = "No data to show"
        Else
            btnNextPostings.Enabled = True
            btnNextPostings.Text = "Next " & CStr(lvRequests.Items.Count - 1) & " Postings"
        End If

        'RESET LVCOUNT TO 0
        lvcount = 0

End Sub

2 个答案:

答案 0 :(得分:2)

来自office.microsoft.access.help

  

TOP谓词不会在相等的值之间进行选择。

因此,如果您的所有记录在[Date-time received]中具有相同的值,则查询将返回所有行。

解决方法
如果您在表ID中有Requests - 字段(或其他字段,但具有唯一值),则在ORDER

中使用它
SELECT TOP 100 * FROM Requests ORDER BY [Date-time received] DESC, [ID] DESC

答案 1 :(得分:0)

只是一种解决方法:如果它不是大数据,你可以将结果输入到某个集合中,然后从该集合中获取前100个项目(我认为使用vb.NET会很容易)并将其他项目移除。