下面的代码应该允许用户搜索特定记录或执行关键字搜索。
换句话说,如果用户只是点击了搜索按钮,数据就会呈现到屏幕上。
问题是,如果您不断点击搜索按钮,相同的记录或记录会不断重复。
有任何想法吗?
请查看代码,非常感谢您的帮助。
Protected Sub bnt_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bnt.Click
Dim txtSearch As String
'OR Perhaps pamametized query will be better
txtSearch = searchBox.Text.Trim()
Dim strSQL As String = ("SELECT fieldname FROM mytable WHERE keycode like '%' + @txtSearch + '%'")
Dim connStr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim conn As New SqlConnection(connStr)
Dim cmd As New SqlCommand(strSQL, conn)
Dim objReader As SqlDataReader
cmd.Parameters.AddWithValue("@txtSearch", strSearch)
conn.Open()
objReader = cmd.ExecuteReader()
DataBind()
While objReader.Read()
With objReader
Dim li As New ListItem(objReader(0).ToString())
url.Items.Add(li)
End With
End While
conn.Close()
End Sub