ASP.NET实现按拼音码模糊查询的方法
整个过程分为两部分:生成拼音码字段、按拼音码进行模糊查询。
批量生成拼音码字段的实现:
protectedvoidButton1_Click1(objectsender,EventArgse)
{
stringstrSQL;
strSQL="selectmcfromTEST001";
IDataReaderdr=dac.DataReaderQuery(strSQL);
while(dr.Read())
{
stringmc=dr["mc"].ToString();
stringpym=StrToPinyin.GetChineseSpell(mc);
if(pym.Length>6)
{
pym=pym.Substring(0,6);//我这里只去了6位,大家可以看自己爱好而定!
}
stringupdateSql="updateTEST001setpym='"+pym+"'wheremc='"+mc+"'";
dac.update(updateSql);
}
dr.Close();
Response.Write("<script>alert('操作成功!');</script>");
}
StrToPinyin类的GetChineseSpell方法(取汉字拼音字母):
publicstaticstringGetChineseSpell(stringstrText)
{
if(strText==null||strText.Length==0)
returnstrText;
System.Text.StringBuildermyStr=newSystem.Text.StringBuilder();
foreach(charvCharinstrText)
{
//若不是汉字则直接输出
if((int)vChar<19968||(int)vChar>40869)
{
myStr.Append(char.ToUpper(vChar));
}
elseif((int)vChar>=19968&&(int)vChar<=40869)
{
//若字符Unicode编码在编码范围则查汉字列表进行转换输出
foreach(stringstrListinstrChineseCharList)
{
if(strList.IndexOf(vChar)>0)
{
myStr.Append(strList[0]);
break;
}
}
}
}
returnmyStr.ToString();
}
按拼音码进行模糊查询:
这个简单了,用select查询,where条件用LIKE即可,相信大家一定都会操作。
相信以后在实现按用户输入的拼音码进行数据的模糊查询功能的时候,大家就可以运用今天所学的ASP.NET实现按拼音码模糊查询了。