如何在Web网站实现搜索功能
以前曾在一些论坛上帖在搜索功能,不过还是有网友对Insus.NET说起不太会用。另外随着时间的推移,Insus.NET再把Search Utility再重构一下,希望用户能更方便容易应用。重构过的Utility,可以从下面这里下载:http://www.cnblogs.com/insus/articles/1999759.html 下载并解压,把InsusSearchUtility.dll文件放入bin目录即可。
把用户搜索字段与值,组成表过式,下面Insus.NET列出一些较常用的表达式:
View Code
InsusSearchUtility objIsu = new InsusSearchUtility();
string key;
string value;
string condition;
//搜索数据库表的字段
key = "[MemberName]";
// 取TextBox的值
value = this.TextBox1.Text.Trim();
//值表过式
condition = string.Format(" LIKE '%{0}%'", value);
//存入对象中
InsusSearchObject sioScrapNumber = new InsusSearchObject(key, value, condition);
//InsusSearchUtility添加一个对象
objIsu.Add(sioScrapNumber);
//DropDownList下拉式选择
if (ddlUnitMeasure.SelectedItem.Value != null)
{
key = "[TitleId]";
value = this.ddlTitle.SelectedItem.Value;
condition = string.Format(" = '{0}'", value);
InsusSearchObject sioTitle = new InsusSearchObject(key, value, condition);
objIsu.Add(sioTitle);
}
//日期搜索
key = "[Birday]";
//下面这个InsusDateTimeUtility可以从这里下载;http://www.cnblogs.com/insus/articles/1735126.html
//可以某一天,输入同一天,也可以是一段日期,两个参数,开始日期与结束日期
value = objInsusDateTimeUtility.GetDateTime(this.DateSelector1.StartDate.Text.Trim(), this.DateSelector1.EndDate.Text.Trim());
//注意一下这个表达式,没有使用谓词,因为上面这个类别已经带出BETWEEN ... AND ...了。
condition = string.Format(" {0}", value);
InsusSearchObject sioBirday = new InsusSearchObject(key, value, condition);
objIsu.Add(sioBirday);
//多选,如CheckBox
key = "[ClassId]";
value = GetCheckBoxSelectedValue(dlAccuracyClass, "CheckBox1");
//搜索整形,注意一下表达式,使用IN谓词, 如"1,5,9,3,2"
condition = string.Format(" IN ({0})", value.Replace("''", "'"));
InsusSearchObject sioClass = new InsusSearchObject(key, value, condition);
objIsu.Add(sioClass);
//多选,如CheckBoxList
key = "[ComputerName]";
value = GetCheckBoxListSelectedValue(this.cblCS);
//搜索字符串,如"hp,lo,yf,wr"
condition = string.Format(" IN ('{0}')", value.Replace(",", "','"));
InsusSearchObject sioComputerName = new InsusSearchObject(key, value, condition);
objIsu.Add(sioComputerName);
string key;
string value;
string condition;
//搜索数据库表的字段
key = "[MemberName]";
// 取TextBox的值
value = this.TextBox1.Text.Trim();
//值表过式
condition = string.Format(" LIKE '%{0}%'", value);
//存入对象中
InsusSearchObject sioScrapNumber = new InsusSearchObject(key, value, condition);
//InsusSearchUtility添加一个对象
objIsu.Add(sioScrapNumber);
//DropDownList下拉式选择
if (ddlUnitMeasure.SelectedItem.Value != null)
{
key = "[TitleId]";
value = this.ddlTitle.SelectedItem.Value;
condition = string.Format(" = '{0}'", value);
InsusSearchObject sioTitle = new InsusSearchObject(key, value, condition);
objIsu.Add(sioTitle);
}
//日期搜索
key = "[Birday]";
//下面这个InsusDateTimeUtility可以从这里下载;http://www.cnblogs.com/insus/articles/1735126.html
//可以某一天,输入同一天,也可以是一段日期,两个参数,开始日期与结束日期
value = objInsusDateTimeUtility.GetDateTime(this.DateSelector1.StartDate.Text.Trim(), this.DateSelector1.EndDate.Text.Trim());
//注意一下这个表达式,没有使用谓词,因为上面这个类别已经带出BETWEEN ... AND ...了。
condition = string.Format(" {0}", value);
InsusSearchObject sioBirday = new InsusSearchObject(key, value, condition);
objIsu.Add(sioBirday);
//多选,如CheckBox
key = "[ClassId]";
value = GetCheckBoxSelectedValue(dlAccuracyClass, "CheckBox1");
//搜索整形,注意一下表达式,使用IN谓词, 如"1,5,9,3,2"
condition = string.Format(" IN ({0})", value.Replace("''", "'"));
InsusSearchObject sioClass = new InsusSearchObject(key, value, condition);
objIsu.Add(sioClass);
//多选,如CheckBoxList
key = "[ComputerName]";
value = GetCheckBoxListSelectedValue(this.cblCS);
//搜索字符串,如"hp,lo,yf,wr"
condition = string.Format(" IN ('{0}')", value.Replace(",", "','"));
InsusSearchObject sioComputerName = new InsusSearchObject(key, value, condition);
objIsu.Add(sioComputerName);
这个类别,它还有一个函数.SearchCondition(),它会把传入的对象组成一句表达式,即是搜索WHERE的条件语句部分。
objIsu.SearchCondition(objIsu);
执行结果: