对于我的大学项目,我正在设计一个程序,该程序可以读取有关动物的 Microsoft Access 数据库并显示数据。我已经设法对其进行了编程,以便我可以按动物名称进行搜索,它会以另一种形式显示记录,但我需要能够搜索其他字段,例如 LatinName 或 AverageWeight,然后显示任何匹配的结果进入一个组合框,然后用户可以选择该组合框,程序将显示代码,例如,如果我要输入 50kg 并且有两条 50kg 的记录,它会同时显示它们,然后让我选择我想要的。
任何帮助或建议将不胜感激,如果您需要更多信息,请随时询问。
Public Class Form1
Private Sub btnsear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsear.Click
If (txtname.Text = "") Then
MsgBox("Invalid Search")
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 = 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(9)
Form2.breedtxt.Text = dt.Rows(0).Item(10)
Form2.lengthtxt.Text = dt.Rows(0).Item(11)
Form2.txtimage.Text = dt.Rows(0).Item(12)
Form2.socialchk.Checked = dt.Rows(0).Item(8)
If dt.Rows(0).Item(8) = True Then
Form2.socialchk.Checked = True
Else
Form2.socialchk.Checked = False
End If
Catch
MsgBox("Item Not Found")
'con.close()
End Try
End If
If (txtopt.Text = "'") Then
Try
Dim newsql As String
newsql = "select * from Animals where AnimalName like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where LatinName like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where Location like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where AverageHeight like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where AverageWeight like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where DietaryNeeds like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where ConservationStatus like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where AverageLifeSpan like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where BreedingSeason like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where AverageLength like " & "'%" & txtopt.Text & "%'"
Catch
End Try
End If
End Sub