Site search in asp .net application
May I know what are the best practices for im开发者_JAVA技巧plementing a site search in ASP .net web app. The user should be able to enter some keywords and get related links with the keyword. I have just started researching. It would be great if you put on your ideas.
If its a small public site, you're better off using Google as your search engine with their Custom Search engine program.
This isn't an option with an internal site that Google can't see, obviously. If your content is in a database, I've heard good things about Lucine. SQL Server 2008's Full Text search features work rather well, too, if you're on that platform. I think that's what SO uses for its search. Or Jeff played with it at one point and had good things to say. Don't know if they're still using it.
It all depends on where your data is stored: database or static pages.
For the former, I'd use Lucene.NET (check out the SubText blog in which the latest release has a good implementation). For the latter I'd use dtSearch to crawl the site to create an index which you can search across.
If you are using database for storing the content, you can use SQL Server Full Text Search Language functionality which is containing bunch of tables and then you can search against that. We used to this approach before.
See for more detail: http://www.simple-talk.com/sql/learn-sql-server/sql-server-full-text-search-language-features/
Here is C# Code
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView2.PageIndex = e.NewPageIndex;
this.Searchmyform();
}
protected void Search(object sender, EventArgs e)
{
this.Searchmyform();
}
private void Searchmyform()
{
using (SqlCommand cmd = new SqlCommand())
{
string sql = "SELECT * FROM [test].[dbo].[myform]";
if (!string.IsNullOrEmpty(txtSearch.Text.Trim()))
{
sql += " where name like '" + txtSearch.Text + "%' or contact like '" + txtSearch.Text + "%'" +
" or email like '" + txtSearch.Text + "%' ";
}
cmd.CommandText = sql;
cmd.Connection = conn;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
}
}
}
Aspx code is such that
<div class="topnav card-body">
<div class="search-container-lg float-right">
<asp:TextBox ID="txtSearch" runat="server"
OnTextChanged="Search"
AutoPostBack="true"
placeholder="Search.."
CssClass="">
</asp:TextBox>
...
精彩评论