当记录数为 0 时退出函数

exit function when number of records in 0

我正在我的列表视图上应用搜索过滤器 (sSQL)。我想在结果没有找到记录时取消操作,否则Listview 是空的,我无法撤消筛选。

Private Sub LoadStandardListview(Optional ByVal sFilter As String = "")

        Try
            Dim sSQL As String
            Dim sSubStr As String
            Dim strs As String
            If sFilter = "" Then
                sSQL = "SELECT Id,[Document Type],[Document Name],DISnummer,XPostnummer,[Last Accessed],[Document Location] FROM Archief WHERE ([Document Type] Is Not Null) AND ([DISnummer] Is Not Null) AND ([XPostnummer] Is Not Null) AND ([Last Accessed] Is Not Null) ORDER BY Id DESC"
            Else
                sSubStr = " AND [Document Name] LIKE '%" + sFilter + "%' "
                sSQL = "SELECT Id,[Document Type],[Document Name],DISnummer,XPostnummer,[Last Accessed],[Document Location] FROM Archief WHERE ([Document Type] Is Not Null) AND ([DISnummer] Is Not Null) AND ([XPostnummer] Is Not Null) AND ([Last Accessed] Is Not Null)" + sSubStr + "ORDER BY Id DESC"
            End If
            strs = "SELECT Count(*) FROM Archief"
            Dim cmd As New OleDb.OleDbCommand(sSQL, con)
            Dim cmd2 As New OleDb.OleDbCommand(strs, con)
            Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader
            Dim dis As String
            Dim docImage As VariantType
            Dim items As New List(Of ListViewItem)
            dr.Read()
            Dim recordCount As Long
            recordCount = cmd2.ExecuteScalar

            ListView1.BeginUpdate()
            ListView1.Items.Clear()
            While dr.Read
            If dr("Document Type").ToString = "PDF" Or dr("Document Type").ToString = "pdf" Then
                docImage = (ImageList1.Images.Count - 1)
            ElseIf dr("Document Type").ToString = "DOC" Or dr("Document Type").ToString = "doc" Then
                docImage = (ImageList1.Images.Count - 3)
            ElseIf dr("Document Type").ToString = "PPT" Or dr("Document Type").ToString = "ppt" Then
                docImage = (ImageList1.Images.Count - 2)
            End If

            dis = dr("Last Accessed").Substring(0, dr("Last Accessed").IndexOf(" "))
            items.Add(New ListViewItem(New String() {"", dr("Document Name").ToString, dr("DISnummer"), dr("XPostnummer"), dis, dr("Id").ToString, dr("Document Location").ToString}, docImage))
        End While
        ListView1.Items.AddRange(items.ToArray)
        ListView1.EndUpdate()
            sNumRec = items.Count
            If sFilter <> "" Then
                ToolStripTextBox3.Text = sNumRec + " Documents"
            Else
                ToolStripTextBox3.Text = recordCount.ToString + " Documents"
            End If
        Catch ex As Exception
        MsgBox(ex.Message)
        End Try
    End Sub

有没有一种方法可以在找不到记录时取消操作并用所有记录再次填充列表视图?

您在哪里使用了 cmd2.ExecuteScalar 的值,即 recordCount?试试这个

If recordCount > 0 Then
   While dr.Read
         ...
   End While
   dr.Close()
Else
   `Refill listview with no filter`
End If
Dim sSQL As String
            Dim sSubStr As String
            If sFilter = "" Then
                sSQL = "SELECT Id,[Document Type],[Document Name],DISnummer,XPostnummer,[Last Accessed],[Document Location] FROM Archief ORDER BY Id DESC"
            Else
                sSubStr = "WHERE [Document Name] LIKE '%" + sFilter + "%' "
                sSQL = "SELECT Id,[Document Type],[Document Name],DISnummer,XPostnummer,[Last Accessed],[Document Location] FROM Archief " + sSubStr + " ORDER BY Id DESC"
            End If
            Dim cmd As New OleDbCommand(sSQL, con)
            Using dr As OleDbDataReader = cmd.ExecuteReader
                Dim dis As String
                Dim docImage As VariantType
                Dim items As New List(Of ListViewItem)

                If dr.HasRows() Then

                    ListView1.BeginUpdate()
                    ListView1.Items.Clear()

                    While dr.Read
                        If dr("Document Type").ToString = "PDF" Or dr("Document Type").ToString = "pdf" Then
                            docImage = (ImageList1.Images.Count - 1)
                        ElseIf dr("Document Type").ToString = "doc" Or dr("Document Type").ToString = "docx" Then
                            docImage = (ImageList1.Images.Count - 3)
                        ElseIf dr("Document Type").ToString = "ppt" Or dr("Document Type").ToString = "pptx" Then
                            docImage = (ImageList1.Images.Count - 2)
                        ElseIf dr("Document Type").ToString = "xls" Or dr("Document Type").ToString = "xlsx" Then
                            docImage = (ImageList1.Images.Count - 4)
                        End If

                        dis = dr("Last Accessed").Substring(0, dr("Last Accessed").IndexOf(" "))
                        items.Add(New ListViewItem(New String() {"", dr("Document Name").ToString, dr("DISnummer").ToString, dr("XPostnummer").ToString, dis, dr("Id").ToString, dr("Document Location").ToString}, docImage))

                    End While
                    ListView1.Items.AddRange(items.ToArray)
                    ListView1.EndUpdate()
                Else
                    MsgBox("Nothing was found.",, Title)
                End If
                dr.Close()

            End Using

我稍微更改了代码。我放

Using dr as New OleDbDataReader = cmd.ExecuteReader
If dr.HasRows() Then