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



由于某种原因,类似的东西不起作用,例如是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
 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 之后包含任何内容的结果
  • 在搜索字符串之前和之后添加它们,以匹配任何包含术语 Ja 的结果

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

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

另外,谷歌"SQL注入",上面的代码是自找麻烦。

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

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

这是"开头为"文本的逻辑。

最新更新