0

我需要让我的程序能够搜索数据库中的每一列,直到找到匹配项,然后将这些匹配项输出到组合框中。我目前已经编写了一种搜索一列(特别是动物名称)的方法,但我需要添加搜索其他列并在组合框中显示多个结果的选项,然后用户可以选择使用程序打开到表格 2,我用来显示数据。

这是我当前正在使用的代码,第一部分用于按动物名称搜索,第二部分用于在可选框中搜索,然后需要将结果显示到组合框中。我还有一个问题,除非名称框中有数据,否则我无法在可选框中搜索,这违背了可选框的目的。任何帮助将不胜感激。

 Private Sub btnsear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsear.Click
    If (txtname.Text = "'") Then
        MsgBox("No information has been entered")
    Else
        Try
            Dim newsql As String
            newsql = "select * from Animals where AnimalName like " & "'%" & txtname.Text & "%'"
            'MsgBox("select * from Animals where AnimalName like " & "'" & txtname.Text & "'")
            'MsgBox(newsql)
            Dim con As New OleDb.OleDbConnection
            Dim da As New OleDb.OleDbDataAdapter

            'Dim ds As NewDataTable
            Dim dt As New DataTable("Animals")
            'uses the 2010 compatible connection string
            con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Kamran\Desktop\College\Computing Project\Animals.accdb" 'h:\Animals.accdb"
            con.Open()

            da = New OleDb.OleDbDataAdapter(newsql, con)
            da.Fill(dt)

            Form2.Show()

            'show name in unbound text box
            Form2.nametxt.Text = dt.Rows(0).Item(1)
            Form2.latintxt.Text = dt.Rows(0).Item(2)
            Form2.locationtxt.Text = dt.Rows(0).Item(3)
            Form2.heighttxt.Text = dt.Rows(0).Item(4)
            Form2.weighttxt.Text = dt.Rows(0).Item(5)
            Form2.diettxt.Text = dt.Rows(0).Item(6)
            Form2.statustxt.Text = dt.Rows(0).Item(7)
            Form2.lifetxt.Text = dt.Rows(0).Item(8)
            Form2.breedtxt.Text = dt.Rows(0).Item(9)
            Form2.lengthtxt.Text = dt.Rows(0).Item(10)
            Form2.txtimage.Text = dt.Rows(0).Item(11)

        Catch
            'MsgBox("Animal Not Found")
            'con.close()
        End Try
    End If


    If txtname.Text = "" Then
        MsgBox("Invalid Search")
    Else
        Try
            Dim newsql As String = "SELECT * FROM Animals WHERE AnimalName LIKE " & "'%" & txtname.Text & "%'"

            If txtopt.Text <> "" Then

                newsql &= " AND (AnimalName LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR LatinName LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR Location LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR AverageHeight LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR AverageWeight LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR DietaryNeeds LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR ConservationStatus LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR AverageLifeSpan LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR BreedingSeason LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR AverageLength LIKE " & "'%" & txtopt.Text & "%')"
            End If

            Dim con As New OleDb.OleDbConnection
            Dim da As New OleDb.OleDbDataAdapter

            Dim dt As New DataTable("Animals")

            con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Kamran\Desktop\College\Computing Project\Animals.accdb" 'h:\Animals.accdb"
            con.Open()

            da = New OleDb.OleDbDataAdapter(newsql, con)
            da.Fill(dt)
    Catch
            'MsgBox("Item Not Found")
            'con.close()
        End Try
    End If
4

1 回答 1

0

如果第二个文本框还包含来自用户的查询数据,请将其附加到您的 sql 语句中,就像您对第一个字段所做的那样:AND Field2 like %User Value%

于 2013-04-14T23:33:48.147 回答