如何在任何字段中进行搜索?

时间:2014-09-18 21:51:17

标签: sql vb.net search

如何使此代码在文本框下搜索任何字段?现在它只会返回一个结果,如果所有字段完全匹配? 提前谢谢!

Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
    cmd = New SqlCommand
    cmd.Connection = cn
    cmd.CommandText = "Select * from [Case Managers] " & _
          "where firstname like '%" & Me.txtfirstname.Text & "%' " &
          "or " & _
          "lastname like '%" & Me.txtlastname.Text & "%' " & _
          "or " & _
          "credentials like '%" & Me.txtCredentials.Text & "%'" & _
          "or " & _
          "active like '%" & Me.cboActive.Text & "%'"
    dr = cmd.ExecuteReader

    Me.ListView1.Items.Clear()
    While dr.Read
        With Me.ListView1
            .Items.Add(dr(0))
            With .Items(.Items.Count - 1).SubItems
                .Add(dr(1))
                .Add(dr(2))
                .Add(dr(3))
                .Add(If(dr(4) = 0, "No", "Yes"))
            End With
        End With
    End While
    dr.Close()
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

您可以使用OR: WHERE field1 like or = whatever1或field2 like或者equal2 ...

http://www.sqlservercentral.com/Forums/Topic750935-338-1.aspx

或者您可以使用concat:连接字段中的结果,并执行类似:

search a whole table in mySQL for a string