转载了好几个地方,很难确定最早的出处。
将源码贴出来先。
1usingSystem;
2usingSystem.Text;
3usingSystem.IO;
4
5namespacets
6{
7classtest
8{
9privatestaticstring[]startChars={"啊","芭","擦","搭","蛾","发","噶","哈",
"击","击","喀","垃","妈","拿","哦","啪","期","然","撒","塌","挖","挖","挖","昔","压","匝"};
10privatestaticstring[]endChars={"澳","怖","错","堕","贰","咐","过","祸",
"啊","骏","阔","络","穆","诺","沤","瀑","群","弱","所","唾","啊","啊","误","迅","孕","座"};
11
12/**////<summary>
13///根据字符和对应的中文字符,转成SQL查询条件
14///</summary>
15///<paramname="cChar">要转化的字符,[A-Z]</param>
16///<paramname="strFieldName">条件左值</param>
17///<returns>SQL条件</returns>
18///<remarks>Sxf2001-1-4*****JY2002-1-4</remarks>
19publicstaticstringGetCharCondition(charcChar,stringstrFieldName)
20{
21stringstrWord;
22intIndex=(int)(char.ToUpper(cChar))-(int)'A';
23if(Index>=0&&Index<26)
24strWord=startChars[Index];
25else
26strWord=startChars[0];
27
28//returnstring.Format("(({0}>='{1}'AND{0}<'[')OR({0}>='{3}'AND{0}<'{{')OR{0}>='{2}')",
29//strFieldName,char.ToUpper(cChar),strWord,char.ToLower(cChar));
30
31returnstring.Format("(({0}>='{3}'AND{0}<='zzzzzzzz')OR{0}>='{2}')",
32strFieldName,char.ToUpper(cChar),strWord,char.ToLower(cChar));
33}
34
35/**////<summary>
36///将指定字段值的每个字符分割,这样可以生成同音查询的SQL
37///</summary>
38///<paramname="fieldName">字段名</param>
39///<paramname="fieldValue">字段值</param>
40///<returns>生成的可以进行同音查询的SQL</returns>
41publicstaticstringGetCharFullCondition(stringfieldName,stringfieldValue)
42{
43StringBuildersql=newStringBuilder(1024);
44inti=1;
45foreach(charcinfieldValue)
46{
47if(i>1)
48sql.Append("AND");
49intindex=(int)(char.ToUpper(c))-(int)'A';
50stringstartWord,endWord;
51if(index>=0&&index<26)
52{
53startWord=startChars[index];
54endWord=endChars[index];
55}
56else
57{
58startWord=startChars[0];
59endWord=endChars[0];
60}
61stringsubStr=String.Format("SUBSTRING({0},{1},{2})",fieldName,i,1);
62sql.AppendFormat("({0}BETWEEN'{1}'AND'{2}')",subStr,startWord,endWord);
63++i;
64}
65
66returnsql.ToString();
67}
68}
69}