Click here to Skip to main content
15,886,578 members
Articles / Database Development / SQL Server

Data Archive

Rate me:
Please Sign up or sign in to vote.
3.60/5 (2 votes)
30 Jun 20043 min read 51.3K   2.2K   33  
This article describes creating indexed search systems using relational databases
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DataArchive
{
	/// <summary>
	/// Summary description for SearchForm.
	/// </summary>
	public class SearchForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txtSearchQuery;
		private System.Windows.Forms.Button btnSearch;
		private System.Windows.Forms.RadioButton rbtnCriteriaAnyWords;
		private System.Windows.Forms.RadioButton rbtnCriteriaAllWords;
		private System.Windows.Forms.RadioButton rbtnCriteriaExactPhrase;
		private System.Windows.Forms.RadioButton rbtnSortRelevance;
		private System.Windows.Forms.RadioButton rbtnSortCategory;
		private System.Windows.Forms.RadioButton rbtnSortName;
		private System.Windows.Forms.RadioButton rbtnSortNone;
		private System.Windows.Forms.RadioButton rbtnCriteriaFullTextQuery;
		private System.Windows.Forms.GroupBox gbSortResults;
		private System.Windows.Forms.GroupBox gbCriteria;
		private System.Windows.Forms.DataGrid dgResults;
		private System.Windows.Forms.Button btnClose;
		
		/// <summary>
		/// 
		/// </summary>
		private SqlHelper sqlHelper;
		/// <summary>
		/// 
		/// </summary>
		public SqlHelper DatabaseHelper
		{
			set
			{
				this.sqlHelper = value;
			}
		}
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SearchForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			
			//
			this.txtSearchQuery.KeyDown += new KeyEventHandler(txtSearchQuery_KeyDown);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SearchForm));
			this.txtSearchQuery = new System.Windows.Forms.TextBox();
			this.btnSearch = new System.Windows.Forms.Button();
			this.rbtnCriteriaAnyWords = new System.Windows.Forms.RadioButton();
			this.rbtnCriteriaAllWords = new System.Windows.Forms.RadioButton();
			this.rbtnCriteriaExactPhrase = new System.Windows.Forms.RadioButton();
			this.rbtnSortRelevance = new System.Windows.Forms.RadioButton();
			this.rbtnSortCategory = new System.Windows.Forms.RadioButton();
			this.rbtnSortName = new System.Windows.Forms.RadioButton();
			this.rbtnSortNone = new System.Windows.Forms.RadioButton();
			this.rbtnCriteriaFullTextQuery = new System.Windows.Forms.RadioButton();
			this.gbSortResults = new System.Windows.Forms.GroupBox();
			this.gbCriteria = new System.Windows.Forms.GroupBox();
			this.dgResults = new System.Windows.Forms.DataGrid();
			this.btnClose = new System.Windows.Forms.Button();
			this.gbSortResults.SuspendLayout();
			this.gbCriteria.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dgResults)).BeginInit();
			this.SuspendLayout();
			// 
			// txtSearchQuery
			// 
			this.txtSearchQuery.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtSearchQuery.Location = new System.Drawing.Point(8, 10);
			this.txtSearchQuery.Name = "txtSearchQuery";
			this.txtSearchQuery.Size = new System.Drawing.Size(342, 20);
			this.txtSearchQuery.TabIndex = 0;
			this.txtSearchQuery.Text = "";
			// 
			// btnSearch
			// 
			this.btnSearch.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.btnSearch.Location = new System.Drawing.Point(355, 10);
			this.btnSearch.Name = "btnSearch";
			this.btnSearch.Size = new System.Drawing.Size(75, 20);
			this.btnSearch.TabIndex = 1;
			this.btnSearch.Text = "Search";
			this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
			// 
			// rbtnCriteriaAnyWords
			// 
			this.rbtnCriteriaAnyWords.Checked = true;
			this.rbtnCriteriaAnyWords.Location = new System.Drawing.Point(25, 15);
			this.rbtnCriteriaAnyWords.Name = "rbtnCriteriaAnyWords";
			this.rbtnCriteriaAnyWords.Size = new System.Drawing.Size(104, 16);
			this.rbtnCriteriaAnyWords.TabIndex = 2;
			this.rbtnCriteriaAnyWords.TabStop = true;
			this.rbtnCriteriaAnyWords.Text = "any words";
			// 
			// rbtnCriteriaAllWords
			// 
			this.rbtnCriteriaAllWords.Location = new System.Drawing.Point(135, 15);
			this.rbtnCriteriaAllWords.Name = "rbtnCriteriaAllWords";
			this.rbtnCriteriaAllWords.Size = new System.Drawing.Size(104, 16);
			this.rbtnCriteriaAllWords.TabIndex = 3;
			this.rbtnCriteriaAllWords.Text = "all words";
			// 
			// rbtnCriteriaExactPhrase
			// 
			this.rbtnCriteriaExactPhrase.Location = new System.Drawing.Point(255, 15);
			this.rbtnCriteriaExactPhrase.Name = "rbtnCriteriaExactPhrase";
			this.rbtnCriteriaExactPhrase.Size = new System.Drawing.Size(104, 16);
			this.rbtnCriteriaExactPhrase.TabIndex = 4;
			this.rbtnCriteriaExactPhrase.Text = "exact phrase";
			// 
			// rbtnSortRelevance
			// 
			this.rbtnSortRelevance.Checked = true;
			this.rbtnSortRelevance.Location = new System.Drawing.Point(25, 15);
			this.rbtnSortRelevance.Name = "rbtnSortRelevance";
			this.rbtnSortRelevance.Size = new System.Drawing.Size(104, 16);
			this.rbtnSortRelevance.TabIndex = 6;
			this.rbtnSortRelevance.TabStop = true;
			this.rbtnSortRelevance.Text = "relevance";
			// 
			// rbtnSortCategory
			// 
			this.rbtnSortCategory.Location = new System.Drawing.Point(135, 15);
			this.rbtnSortCategory.Name = "rbtnSortCategory";
			this.rbtnSortCategory.Size = new System.Drawing.Size(104, 16);
			this.rbtnSortCategory.TabIndex = 7;
			this.rbtnSortCategory.Text = "category";
			// 
			// rbtnSortName
			// 
			this.rbtnSortName.Location = new System.Drawing.Point(255, 15);
			this.rbtnSortName.Name = "rbtnSortName";
			this.rbtnSortName.Size = new System.Drawing.Size(104, 16);
			this.rbtnSortName.TabIndex = 8;
			this.rbtnSortName.Text = "name";
			// 
			// rbtnSortNone
			// 
			this.rbtnSortNone.Location = new System.Drawing.Point(370, 15);
			this.rbtnSortNone.Name = "rbtnSortNone";
			this.rbtnSortNone.Size = new System.Drawing.Size(104, 16);
			this.rbtnSortNone.TabIndex = 9;
			this.rbtnSortNone.Text = "none";
			// 
			// rbtnCriteriaFullTextQuery
			// 
			this.rbtnCriteriaFullTextQuery.Location = new System.Drawing.Point(370, 15);
			this.rbtnCriteriaFullTextQuery.Name = "rbtnCriteriaFullTextQuery";
			this.rbtnCriteriaFullTextQuery.Size = new System.Drawing.Size(104, 16);
			this.rbtnCriteriaFullTextQuery.TabIndex = 5;
			this.rbtnCriteriaFullTextQuery.Text = "full-text";
			// 
			// gbSortResults
			// 
			this.gbSortResults.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.gbSortResults.Controls.Add(this.rbtnSortRelevance);
			this.gbSortResults.Controls.Add(this.rbtnSortCategory);
			this.gbSortResults.Controls.Add(this.rbtnSortName);
			this.gbSortResults.Controls.Add(this.rbtnSortNone);
			this.gbSortResults.Location = new System.Drawing.Point(8, 70);
			this.gbSortResults.Name = "gbSortResults";
			this.gbSortResults.Size = new System.Drawing.Size(504, 35);
			this.gbSortResults.TabIndex = 10;
			this.gbSortResults.TabStop = false;
			this.gbSortResults.Text = "Sort results by";
			// 
			// gbCriteria
			// 
			this.gbCriteria.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.gbCriteria.Controls.Add(this.rbtnCriteriaFullTextQuery);
			this.gbCriteria.Controls.Add(this.rbtnCriteriaExactPhrase);
			this.gbCriteria.Controls.Add(this.rbtnCriteriaAllWords);
			this.gbCriteria.Controls.Add(this.rbtnCriteriaAnyWords);
			this.gbCriteria.Location = new System.Drawing.Point(8, 35);
			this.gbCriteria.Name = "gbCriteria";
			this.gbCriteria.Size = new System.Drawing.Size(504, 35);
			this.gbCriteria.TabIndex = 11;
			this.gbCriteria.TabStop = false;
			this.gbCriteria.Text = "Search criteria";
			// 
			// dgResults
			// 
			this.dgResults.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.dgResults.DataMember = "";
			this.dgResults.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dgResults.Location = new System.Drawing.Point(8, 115);
			this.dgResults.Name = "dgResults";
			this.dgResults.ReadOnly = true;
			this.dgResults.Size = new System.Drawing.Size(504, 180);
			this.dgResults.TabIndex = 10;
			// 
			// btnClose
			// 
			this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnClose.Location = new System.Drawing.Point(435, 10);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(75, 20);
			this.btnClose.TabIndex = 13;
			this.btnClose.Text = "Close";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// SearchForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnClose;
			this.ClientSize = new System.Drawing.Size(520, 301);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.dgResults);
			this.Controls.Add(this.btnSearch);
			this.Controls.Add(this.txtSearchQuery);
			this.Controls.Add(this.gbSortResults);
			this.Controls.Add(this.gbCriteria);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "SearchForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Search for catalog/directory/file";
			this.gbSortResults.ResumeLayout(false);
			this.gbCriteria.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dgResults)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnSearch_Click(object sender, System.EventArgs e)
		{
			// Check the search query text.
			string strText = this.txtSearchQuery.Text;
			if (strText.Length <= 0)
			{
				MessageBox.Show("Enter the search query");
				return;
			}
			
			if (this.sqlHelper == null)
			{
				throw new ApplicationException("DatabaseHelper object is not initialized.");
			}
			
			// Form the search criteria.
			SearchCriteria searchCriteria = SearchCriteria.FullText;
			if (this.rbtnCriteriaAnyWords.Checked)
			{
				searchCriteria = SearchCriteria.AnyWords;
			}
			else if (this.rbtnCriteriaAllWords.Checked)
			{
				searchCriteria = SearchCriteria.AllWords;
			}
			else if (this.rbtnCriteriaExactPhrase.Checked)
			{
				searchCriteria = SearchCriteria.ExactPhrase;
			}
			
			// Form the sort criteria.
			SortCriteria sortCriteria = SortCriteria.None;
			if (this.rbtnSortRelevance.Checked)
			{
				sortCriteria = SortCriteria.Relevance;
			}
			else if (this.rbtnSortCategory.Checked)
			{
				sortCriteria = SortCriteria.Category;
			}
			else if (this.rbtnSortName.Checked)
			{
				sortCriteria = SortCriteria.Name;
			}
			
			// Init the data grid caption text.
			this.dgResults.CaptionText = "";
		
			FullTextQuery queryFolmer = new FullTextQuery();
			
			try
			{
				// Perform the search.
				DataView dv = this.sqlHelper.Search(
					queryFolmer.FormSearchQuery(strText, searchCriteria), sortCriteria);
				this.dgResults.DataSource = dv;
					
				// Set the data grid caption text.
				this.dgResults.CaptionText = "Found results [" + dv.Count.ToString() + "]";
			}
			catch(Exception exp)
			{	
				MessageBox.Show("Error: " + exp.Message, Application.ProductName,
					MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void txtSearchQuery_KeyDown(object sender, KeyEventArgs e)
		{
			// Determine whether the keystroke is a Enter key.
			if (e.KeyCode == Keys.Enter)
			{
				// Simulate that user click on search button.
				this.btnSearch.PerformClick();
			}
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Bulgaria Bulgaria
I am a senior developer in Melon Technologies Ltd.

Comments and Discussions