开发者

search database SQL like query

开发者 https://www.devze.com 2023-02-22 16:33 出处:网络
I have created a bookshop website with database, I need a search engine that will search the database records and present the user with results according to the keyword.

I have created a bookshop website with database, I need a search engine that will search the database records and present the user with results according to the keyword.

I have created the page with the text search box and search button

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
    Search
</h2>
<p>
    Search box.
</p>

<table border="0" cellpadding=5 bgcolor=><tr> <td align="center"> 
<asp:TextBox ID="search_box" runat="server" Width="200px"></asp:TextBox>&nbsp;&nbsp; 
<asp:Button ID="Button1" runat="server" Text="Sea开发者_Go百科rch" Height="19px" 
    Width="75px" />
</td> 
</tr>
</table> 
    <div style="text-align: center">
        <br />
        <span id="Span1" runat="Server" style="Color:Red"></span>
    </div>

and this is the code behind so far

Partial Class Search
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    SELECT * FROM BOOKS
        WHERE Title like '%"search_box.text"%'


End Sub

End Class


VB.Net doesn't understand SQL... it's not as easy as just giving it a SQL Query; you need to use some type of data access layer, probably ADO.Net, and specify which SQL Server your application should connect to, how it should connect to it, etc.

I'd recommend running through a simple tutorial on connecting to SQL through VB.Net.

http://www.fryan0911.com/2009/05/vbnet-tutorial-sql-database-basics.html

-- EDIT --

Now that you're connecting to the database, it will be easiest for you to use one of the built in ASP.Net controls to display the results of your query on your web page. The easiest way to do this will be with a "GridView" control... here's another tutorial that should get you up and running...

http://www.devx.com/dotnet/Article/22141


You're looking for something like this...

Dim SearchText As String = YourTextBox.Text
Dim ds As New DataSet

Using cnn As New SqlConnection("YourConnectionString")
    cnn.Open()
    Using cmd As SqlCommand = cnn.CreateCommand
        cmd.CommandType = "text"
        cmd.CommandText = String.Format("SELECT* FROM BOOKS WHERE Title LIKE '{0}'", SearchText)
        Using da As New SqlDataAdapter
            da.SelectCommand = cmd
            da.Fill(ds)
        End Using
    End Using
End Using

SomeGridControl.DataSource = ds.Tables(0)
SomeGridControl.Databind()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号