我正在创建类似工作的数据库。我是一名IT技术人员,我们有许多计算机可以进行重新映像。我们失去了很多,所以我决定建立一个“数据库”来记录所有内容。这些计算机带有故障单,我希望用户能够通过故障单号或序列号进行搜索。我的应用程序告诉我,当字段为空时,它告诉我何时没有结果,但我无法弄清楚如何使它如果它找到结果,我希望它显示文本框中该行的所有字段。如果它找到多个结果,我希望它显示最新的结果(行也包括它被引入的那一天)。这是我现在拥有的:
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim cmd As OleDbCommand
If txtSearch.Text = "" Then
MsgBox("No input", MsgBoxStyle.Exclamation, "Search")
txtSearch.Focus()
ElseIf InStr(txtSearch.Text, "'") Then
MsgBox("Invalid character: '", MsgBoxStyle.Critical, "Search")
txtSearch.Text = ""
txtSearch.Focus()
ElseIf rdoTicketNumber.Checked = True Then
Dim sql = "select * from Tickets where Ticket_Number = '" & txtSearch.Text & "'"
cmd = New OleDbCommand(sql, con)
con.ConnectionString = ("provider=microsoft.jet.oledb.4.0;data source=../Database.mdb")
con.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
Try
If dr.Read = False Then
MsgBox("No Results", MsgBoxStyle.Information, "Search")
txtSearch.Text = ""
txtSearch.Focus()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
ElseIf rdoSerialNumber.Checked = True Then
Dim sql = "select * from Tickets where Asset_Serial = '" & txtSearch.Text & "'"
cmd = New OleDbCommand(sql, con)
con.ConnectionString = ("provider=microsoft.jet.oledb.4.0;data source=../Database.mdb")
con.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
Try
If dr.Read = False Then
MsgBox("No Results", MsgBoxStyle.Information, "Search")
txtSearch.Text = ""
txtSearch.Focus()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
End If
End Sub
感谢任何和所有帮助。提前谢谢!
答案 0 :(得分:0)
如果您真的只想过滤查询字符串的结果:
Dim sql = "select * from Tickets where Asset_Serial = '" & txtSearch.Text & "'"
要过滤最近的一个,请将其更改为以下内容:
Dim sql = "select TOP(1) * from Tickets where Asset_Serial = '" & txtSearch.Text & "' ORDER BY <yourDateField> DESC"
您应该将<yourDateField>
更改为Tickets
表中该日期字段的名称,如果您只有一天的故障单,并且想要将结果过滤到该日期的最新日期我建议你这个:
Dim sql = "select TOP(1) * from Tickets where Asset_Serial = '" & txtSearch.Text & "' ORDER BY <yourDateField> DESC, <yourPKField> DESC"