通过LIKE搜索不在数据库vb.net中工作

时间:2014-11-24 18:04:37

标签: database vb.net local-database

由于某种原因,喜欢不起作用,例如Jason和我搜索Ja' jason'应该出现它没有。我的代码有问题吗?这是在本地数据库中,也许有帮助吗?

 Private Sub BTN_Search_Click(sender As Object, e As EventArgs) Handles BTN_Search.Click
    'If txtbox is blank then show all records, else do the search by first name.
    If TBX_Search.Text = "" Then
        DoctorsDataGridView.DataSource = Me.RecordsDataSet.Doctors.Select("FirstName LIKE'" & "%" & "'")
    Else
        DoctorsDataGridView.DataSource = Me.RecordsDataSet.Doctors.Select("FirstName LIKE'" & TBX_Search.Text & "'")
    End If
 End Sub

3 个答案:

答案 0 :(得分:1)

%表示部分或缺失的部分,您最后仍然需要它(对于您的情况)。

DoctorsDataGridView.DataSource = Me.RecordsDataSet.Doctors.Select("FirstName LIKE'" & TBX_Search.Text & "%'")

另外,谷歌“SQL注入”,上面的代码是在寻找麻烦。

答案 1 :(得分:1)

 Private Sub BTN_Search_Click(sender As Object, e As EventArgs) Handles BTN_Search.Click
      'If txtbox is blank then show all records, else do the search by first name.
      If TBX_Search.Text = "" Then
           DoctorsDataGridView.DataSource = Me.RecordsDataSet.Doctors.Select("FirstName LIKE '%'")
      Else
           DoctorsDataGridView.DataSource = Me.RecordsDataSet.Doctors.Select("FirstName LIKE '%" & TBX_Search.Text & "%'")
      End If
 End Sub

在您的代码中,您告诉数据库获取与 Ja 相匹配的文章,而不是寻找 Ja%(及其背后的任何内容)

  • 在搜索字符串前添加,以便在 Ja
  • 前面显示任何内容
  • 在搜索字符串后添加,以便在 Ja
  • 之后允许任何结果
  • 在searchstring之前和之后添加它们以匹配任何带有 Ja 一词的结果

答案 2 :(得分:1)

看起来你需要Else逻辑上的通配符:

  

DoctorsDataGridView.DataSource =   Me.RecordsDataSet.Doctors.Select(“FirstName LIKE'”& TBX_Search.Text&   “%”“)

这是“以...开头”文字的逻辑。