Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / SQL

Implementation Example of Ranked Search in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.59/5 (10 votes)
16 Oct 2012LGPL37 min read 58.3K   2.2K   28  
Description how to get started with your own search engine using full text search and SQL-Server 2012.
  • WebTest.zip
    • packages
      • AjaxControlToolkit.4.1.60623
        • AjaxControlToolkit.4.1.60623.nupkg
        • content
          • web.config.transform
        • lib
          • 35
            • AjaxControlToolkit.dll
            • AjaxControlToolkit.pdb
            • ar
              • AjaxControlToolkit.resources.dll
            • cs
              • AjaxControlToolkit.resources.dll
            • de
              • AjaxControlToolkit.resources.dll
            • es
              • AjaxControlToolkit.resources.dll
            • fr
              • AjaxControlToolkit.resources.dll
            • he
              • AjaxControlToolkit.resources.dll
            • hi
              • AjaxControlToolkit.resources.dll
            • it
              • AjaxControlToolkit.resources.dll
            • ja
              • AjaxControlToolkit.resources.dll
            • ko
              • AjaxControlToolkit.resources.dll
            • nl
              • AjaxControlToolkit.resources.dll
            • pt
              • AjaxControlToolkit.resources.dll
            • ru
              • AjaxControlToolkit.resources.dll
            • SanitizerProviders
              • SanitizerProviders.dll
            • tr-TR
              • AjaxControlToolkit.resources.dll
            • zh-CHS
              • AjaxControlToolkit.resources.dll
            • zh-CHT
              • AjaxControlToolkit.resources.dll
          • 40
            • AjaxControlToolkit.dll
            • AjaxControlToolkit.pdb
            • ar
              • AjaxControlToolkit.resources.dll
            • cs
              • AjaxControlToolkit.resources.dll
            • de
              • AjaxControlToolkit.resources.dll
            • es
              • AjaxControlToolkit.resources.dll
            • fr
              • AjaxControlToolkit.resources.dll
            • he
              • AjaxControlToolkit.resources.dll
            • hi
              • AjaxControlToolkit.resources.dll
            • it
              • AjaxControlToolkit.resources.dll
            • ja
              • AjaxControlToolkit.resources.dll
            • ko
              • AjaxControlToolkit.resources.dll
            • nl
              • AjaxControlToolkit.resources.dll
            • pt
              • AjaxControlToolkit.resources.dll
            • ru
              • AjaxControlToolkit.resources.dll
            • SanitizerProviders
              • HtmlAgilityPack.dll
              • SanitizerProviders.dll
            • tr-TR
              • AjaxControlToolkit.resources.dll
            • zh-CHS
              • AjaxControlToolkit.resources.dll
            • zh-CHT
              • AjaxControlToolkit.resources.dll
      • repositories.config
    • WebTest.sln
    • WebTest
  • WebTest.zip
    • AjaxControlToolkit.4.1.60623.nupkg
    • web.config.transform
    • AjaxControlToolkit.dll
    • AjaxControlToolkit.pdb
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • SanitizerProviders.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.dll
    • AjaxControlToolkit.pdb
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • HtmlAgilityPack.dll
    • SanitizerProviders.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • AjaxControlToolkit.resources.dll
    • repositories.config
    • WebTest.sln
    • packages.config
    • AssemblyInfo.cs
    • search.aspx
    • search.aspx.cs
    • search.aspx.designer.cs
    • SearchService.asmx
    • SearchService.asmx.cs
    • Web.config
    • Web.Debug.config
    • Web.Release.config
    • WebTest.csproj
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebTest
{
    public partial class search : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            DataTable dt = SearchDB(Server.HtmlDecode(SearchBox.Text));
            if (dt == null)
                return;
            var row = new TableRow();
            //Add the column headers
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                var headerCell = new TableHeaderCell {Text = dt.Columns[j].ColumnName};
                row.Cells.Add(headerCell);
            }

            ResultTable.Rows.Add(row);

            //Add the row values
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                row = new TableRow();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    var cell = new TableCell {Text = dt.Rows[i][j].ToString()};
                    row.Cells.Add(cell);
                }
                // Add the TableRow to the Table
                ResultTable.Rows.Add(row);
            }
        }

        /// <summary>
        /// Searches the database in all columns and returns the result as a datatable.
        /// </summary>
        /// <param name="searchString">Search String</param>
        /// <returns>DataTable with the results all columns ordered in rank</returns>
        public static DataTable SearchDB(string searchString)
        {
            if (searchString == null)
                return null;
            //Clean Up search string to avoid SQL Injection
            var reg = new Regex(@"[^\w(@)\|&]");
            searchString = reg.Replace(searchString, "");
            searchString = searchString.Trim();
            if (searchString == "")
                return null;

            //The search string
            var dt = new DataTable();
            using (
                var connection =
                    new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString))
            {
                var userDataset = new DataSet();
                var myDataAdapter = new SqlDataAdapter(
                    "SELECT TOP(20) * FROM FREETEXTTABLE(dbo.Names, *, @param) AS r INNER JOIN Names ON r.[KEY] = Names.MyKey order by RANK DESC",
                    connection);
                myDataAdapter.SelectCommand.Parameters.Add("@param", SqlDbType.VarChar, 255);
                myDataAdapter.SelectCommand.Parameters["@param"].Value = searchString;
                myDataAdapter.Fill(dt);
            }
            return dt;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer SharpedNET
Sweden Sweden
I have a development company called SharpredNET focusing on smaller project with high delivery precision on customer need and time. The solution always uses modern frameworks, is good looking and easy to use.

I am also a reseracher in Production System giving me specialization on implementation for manufacturing industry.

Comments and Discussions