在DataGridView上过滤查询

时间:2015-06-24 21:08:47

标签: vb.net ms-access datagridview oledb

我正在尝试构建一个多参数查询来过滤DataGridView上的信息。使用Visual Studio上的QUERY WIZARD生成以下代码。

我遇到的问题是代码不会失败,但永远不会返回结果。我很确定问题是在查询结束时使用的布尔值,但我不完全确定。

我将代码称为过滤,如下所示:

Private Sub FILTER_Accept_Button_Click(sender As Object, e As EventArgs) Handles FILTER_Accept_Button.Click
    Try
        Me.SparesTableAdapter.RunFilter(Me.Spares_DatabaseDataSet.Spares, Name_Of_PersonToolStripTextBox.Text, Site_NameToolStripTextBox.Text, Filter_Supplier_textbox.Text, FilterPartNo.Text, FilterJobNo.Text, FilterOrdered.Checked, FilterInvoiceRecieved.Checked, FilterPaidInvoices.Checked, FilterReceivedItems.Checked, FilterUrgent.Checked)

    Catch ex As System.Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try
End Sub

以上代码调用RunFilter函数:

Public Overridable Overloads Function RunFilter(ByVal dataTable As Spares_DatabaseDataSet.SparesDataTable, ByVal Name_Of_Person As String, ByVal Site_Name As String, ByVal Spares_Supplier As String, ByVal Part_Number As String, ByVal SAP_Job_Number As String, ByVal Date_Ordered As Boolean, ByVal Invoice_Received As Boolean, ByVal Invoice_Paid As Boolean, ByVal Date_Item_Received As Boolean, ByVal Marked_As_Urgent As Boolean) As Integer
            Me.Adapter.SelectCommand = Me.CommandCollection(6)
            If (Name_Of_Person Is Nothing) Then
                Me.Adapter.SelectCommand.Parameters(0).Value = Global.System.DBNull.Value
            Else
                Me.Adapter.SelectCommand.Parameters(0).Value = CType(Name_Of_Person, String)
            End If
            If (Site_Name Is Nothing) Then
                Me.Adapter.SelectCommand.Parameters(1).Value = Global.System.DBNull.Value
            Else
                Me.Adapter.SelectCommand.Parameters(1).Value = CType(Site_Name, String)
            End If
            If (Spares_Supplier Is Nothing) Then
                Me.Adapter.SelectCommand.Parameters(2).Value = Global.System.DBNull.Value
            Else
                Me.Adapter.SelectCommand.Parameters(2).Value = CType(Spares_Supplier, String)
            End If
            If (Part_Number Is Nothing) Then
                Me.Adapter.SelectCommand.Parameters(3).Value = Global.System.DBNull.Value
            Else
                Me.Adapter.SelectCommand.Parameters(3).Value = CType(Part_Number, String)
            End If
            If (SAP_Job_Number Is Nothing) Then
                Me.Adapter.SelectCommand.Parameters(4).Value = Global.System.DBNull.Value
            Else
                Me.Adapter.SelectCommand.Parameters(4).Value = CType(SAP_Job_Number, String)
            End If
            If (Date_Ordered = False) Then
                Me.Adapter.SelectCommand.Parameters(5).Value = CType("", String)
            Else
                Me.Adapter.SelectCommand.Parameters(5).Value = CType("Not Completed", String)
            End If
            If (Invoice_Received = False) Then
                Me.Adapter.SelectCommand.Parameters(6).Value = CType("", String)
            Else
                Me.Adapter.SelectCommand.Parameters(6).Value = CType("Not Completed", String)
            End If
            If (Invoice_Paid = False) Then
                Me.Adapter.SelectCommand.Parameters(7).Value = CType("", String)
            Else
                Me.Adapter.SelectCommand.Parameters(7).Value = CType("Not Completed", String)
            End If
            If (Date_Item_Received = False) Then
                Me.Adapter.SelectCommand.Parameters(8).Value = CType("", String)
            Else
                Me.Adapter.SelectCommand.Parameters(8).Value = CType("Not Completed", String)
            End If

            If (Marked_As_Urgent = False) Then
                Me.Adapter.SelectCommand.Parameters(9).Value = CType(False, Boolean)
            Else
                Me.Adapter.SelectCommand.Parameters(9).Value = CType(Marked_As_Urgent, Boolean)
            End If

            If (Me.ClearBeforeFill = True) Then
                dataTable.Clear()
            End If
            Dim returnValue As Integer = Me.Adapter.Fill(dataTable)
            Return returnValue
        End Function

正在使用的查询(在Me.CommandCollection(6)中):

"SELECT ID, [Name Of Person], [SAP Job Number], [Site Name], [Asset Description], [Spares Supplier], [Supplier Contact Name], [Supplier Contact Phone Number], [Supplier Contact Email], [Spares Description], [Part Number], [Quantity To Order], Cost, Comments, [Request Date], [Date Ordered], [Ordered By], [Invoice Received], [Invoice Paid], [Method Of Payment], [Date Item Received], [Quote Attatchment], [Marked As Urgent]  FROM Spares  WHERE ([Name Of Person] LIKE ? + '%') AND ([Site Name] LIKE ? + '%') AND ([Spares Supplier] LIKE ? + '%') AND ([Part Number] LIKE ? + '%') AND ([SAP Job Number] = ?) AND([Date Ordered] = ?) AND ([Invoice Received] = ?) AND ([Invoice Paid] = ?) AND ([Date Item Received] = ?) AND ([Marked As Urgent] = ?)"

上述查询或许在某种程度上是不正确的?任何指针都会很棒。

感谢。

更新1

我已经更新了我的代码,我决定只编写自己的过滤字符串:

Public Sub Call_Filter()

    Dim myCon = New OleDbConnection(My.Settings.Database_string)

    Try
        myCon.Open()
    Catch ex As Exception
        MsgBox("Sorry, An Error Occurred" & vbNewLine & _
               "Database Not Found!" & vbNewLine & vbNewLine & _
               "Error Message: " & ex.Message, MsgBoxStyle.OkOnly, "Could Not Locate Database")
        Main.VIEW_SavingMessage.Visible = False
        Exit Sub
    End Try

    Dim SQRText As String = "SELECT ID, [Name Of Person], [SAP Job Number], [Site Name], [Asset Description], [Spares Supplier], [Supplier Contact Name], [Supplier Contact Phone Number], " & _
                             "[Supplier Contact Email], [Spares Description], [Part Number], [Quantity To Order], Cost, Comments, [Request Date], [Date Ordered], [Ordered By], [Invoice Received], " & _
                             "[Invoice Paid], [Method Of Payment], [Date Item Received], [Quote Attatchment], [Marked As Urgent] " & _
    "FROM Spares " & _
    "WHERE ([Name Of Person] = @NameOfPerson OR @NameOfPerson = '' OR @NameOfPerson IS NULL) AND " & _
    "([SAP Job Number] = @SAPJobNo OR @SAPJobNo = '' OR @SAPJobNo IS NULL)"


    Dim sqr As OleDbCommand = New OleDbCommand(SQRText, myCon)

    sqr.Parameters.Add("@NameOfPerson", OleDbType.VarChar).Value = Main.Name_Of_PersonToolStripTextBox.Text
    If Main.FilterJobNo.Text <> "" Then
        sqr.Parameters.Add("@SAPJobNo", OleDbType.Integer).Value = Main.FilterJobNo.Text
    Else
        sqr.Parameters.Add("@SAPJobNo", OleDbType.Integer).Value = DBNull.Value
    End If

    Try
        Dim command = sqr.ExecuteNonQuery()
    Catch ex As Exception
        MsgBox("Sorry, An error occured!" & vbNewLine & _
   "The database rejected the Delete request" & vbNewLine & vbNewLine & _
   "Error Message: " & ex.Message, MsgBoxStyle.OkOnly, "Database Refused Parameterised Query")
        Exit Sub
    End Try

    Main.SparesTableAdapter.Fill(Main.Spares_DatabaseDataSet.Spares)


    myCon.Close()

End Sub

这样做有效,但在填充时不会更新绑定到Datagridview1的{​​{1}}。我在这里错过了什么吗?

由于

0 个答案:

没有答案