Visual Studio数据库自定义条件搜索不起作用

时间:2013-07-13 00:22:50

标签: database vb.net visual-studio

我在Visual Studio中创建了一个数据库,我正在使用VB.net进行编码我创建了文本框和复选框,以匹配按下搜索按钮时每个搜索的字段。

每当我使用文本框和复选框执行搜索时,我都会收到错误消息。  Item NameRoomBrokenIn UsefloorNameSearchRoomSearch中的文字搜索到的字段,BrokenSearchareInUseSearchFloorSearch分别......等等。

这是搜索按钮的代码

    Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click

    RecordDataGridView.Refresh()
    Me.RecordBindingSource.Filter = "[Item Name]= '" & NameSearch.Text & "' And  [Room]= '" & RoomSearch.Text & "' And  [Make]= '" & MakeSearch.Text & _
        "' And  [Broken]= '" & BrokenSearch.CheckState & "' And  [Replaced]= '" & ReplacedSearch.CheckState & "'And [ID#]= '" & IdentificationNumberSearch.Text & _
        "' And   [Floor]= '" & FloorSearch.Text & "' And   [In Use]= '" & InUseSearch.CheckState & "'"
    Me.RecordTableAdapter.Fill(Me.MLGDatabaseDataSet.Record)
    RecordDataGridView.Refresh()

End Sub

Data area

Search area

错误

例如我在项目nameSearch和floorSearch中输入文本并按搜索,因为其他文本框中没有文本,所以不会显示任何结果。

2 个答案:

答案 0 :(得分:2)

如果不解决其他问题,例如使用参数化查询来阻止SQL注入或使用StringBuilder更有效地执行连接,我相信您的问题可能是此代码段中缺少的空间:

ReplacedSearch.CheckState & "'And [ID#]= '"

如果您将其更改为

ReplacedSearch.CheckState & "' And [ID#]= '"

它可以解决即时错误。但是,您几乎肯定会在中间的OR语句中引入其他逻辑错误(您可能希望包围使用括号进行OR化的两个子句)。

答案 1 :(得分:0)

我发现了与@competent_tech相同的东西。我会这样设置它以便更容易调试。

    Dim strFilter As String = _
            "[Item Name]= '" & NameSearch.Text & _
            "' And  [Room]= '" & RoomSearch.Text & _
            "' And  [Make]= '" & MakeSearch.Text & _
            "' And  [Broken]= '" & BrokenSearch.CheckState & _
            "' Or  [Replaced]= '" & ReplacedSearch.CheckState & _
            "' And [ID#]= '" & IdentificationNumberSearch.Text & _
            "' And   [Floor]= '" & FloorSearch.Text & _
            "' And   [In Use]= '" & InUseSearch.CheckState & "'"
    Debug.Print(strFilter)
    RecordBindingSource.Filter = strFilter

编辑:您只想在有条件的情况下进行过滤

    'filter string
    Dim strFilter As String = ""

    'for check boxes you probably want to filter only if checked
    If BrokenSearch.CheckState = CheckState.Checked Then strFilter += "And [Broken]= " & BrokenSearch.CheckState & " "
    If ReplacedSearch.CheckState = CheckState.Checked Then strFilter += "And [Replaced]= " & ReplacedSearch.CheckState & " "
    If InUseSearch.CheckState = CheckState.Checked Then strFilter += "And [In Use]= " & InUseSearch.CheckState & " "

    'for text boxes you want to filter only if has text
    If IdentificationNumberSearch.Text.Length > 0 Then strFilter += "And [ID#]= '" & IdentificationNumberSearch.Text & "' "
    If FloorSearch.Text.Length > 0 Then strFilter += "And [Floor]= " & FloorSearch.Text & " "
    If RoomSearch.Text.Length > 0 Then strFilter += "And [Room]= " & RoomSearch.Text & " "
    If MakeSearch.Text.Length > 0 Then strFilter += "And [Make]= '" & MakeSearch.Text & "' "
    If NameSearch.Text.Length > 0 Then strFilter += "And [Item Name]= '" & NameSearch.Text & "' "

    'check to make sure there is at least one condition
    If strFilter.Length > 0 Then

        'remove the first "And"
        strFilter = strFilter.Remove(0, 4)

        'output
        Debug.Print(strFilter)

        'set to filter
        RecordBindingSource.Filter = strFilter
    End If

编辑:您的错误“无法对System.Boolean和System.String执行'='操作”我认为问题是过滤器值必须与字段类型匹配。

对于字符串,请执行以下操作:[Make] = 'stringValue'使用单引号; 对于整数,请执行以下操作:[Floor] = 24,不带单引号; 有点这样做:[Broken] = 1没有单引号;

在数据库中看起来像Broken,Replaced和In Use是Bit类型。而且我猜测地板和房间是整数。