鱼遇于池,池涸,相濡以沫,相鞠以湿,不若相忘于海。

while (alive) {
  object state = working & fighting & enjoying & living thanksgiving;
}
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

按拼音模糊匹配查询条件的生成类

Posted on 2005-07-18 23:39  alittlefish  阅读(697)  评论(1编辑  收藏  举报
转载了好几个地方,很难确定最早的出处。
将源码贴出来先。
 1using System; 
 2using System.Text; 
 3using System.IO; 
 4
 5namespace ts
 6
 7    class test
 8    
 9        private static string[] startChars = {"""""","","","","","",
"","","","","","","","","","""","","","","","","",""}

10        private static string[] endChars = {"""""","","","","","",
"","","","","","","","","","""","","","","","","",""}

11
12        /// <summary> 
13        /// 根据字符和对应的中文字符,转成SQL查询条件 
14        /// </summary> 
15        /// <param name="cChar">要转化的字符,[A-Z]</param> 
16        /// <param name="strFieldName">条件左值</param> 
17        /// <returns>SQL条件</returns> 
18        /// <remarks> Sxf 2001-1-4 ***** JY 2002-1-4 </remarks> 

19        public static string GetCharCondition(char cChar, string strFieldName) 
20        
21            string strWord; 
22            int Index = (int)(char.ToUpper(cChar)) - (int)'A'
23            if (Index >= 0 && Index < 26
24                strWord = startChars[Index]; 
25            else 
26                strWord = startChars[0]; 
27            
28            //return string.Format("(({0}>='{1}' AND {0}<'[') OR ({0} >= '{3}' AND {0} < '{{') OR {0}>='{2}')",  
29            //strFieldName, char.ToUpper(cChar), strWord, char.ToLower(cChar)); 
30            
31            return string.Format("(({0} >= '{3}' AND {0} <= 'zzzzzzzz') OR {0}>='{2}')",  
32            strFieldName, char.ToUpper(cChar), strWord, char.ToLower(cChar)); 
33        }
 
34
35        /// <summary> 
36        /// 将指定字段值的每个字符分割,这样可以生成同音查询的SQL 
37        /// </summary> 
38        /// <param name="fieldName">字段名</param> 
39        /// <param name="fieldValue">字段值</param> 
40        /// <returns>生成的可以进行同音查询的SQL</returns> 

41        public static string GetCharFullCondition(string fieldName, string fieldValue) 
42        
43            StringBuilder sql = new StringBuilder(1024); 
44            int i = 1
45            foreach (char c in fieldValue) 
46            
47                if (i > 1
48                    sql.Append(" AND "); 
49                int index = (int)(char.ToUpper(c)) - (int)'A'
50                string startWord, endWord; 
51                if (index >= 0 && index < 26
52                
53                    startWord = startChars[index]; 
54                    endWord = endChars[index]; 
55                }
 
56                else 
57                
58                    startWord = startChars[0]; 
59                    endWord = endChars[0]; 
60                }
 
61                string subStr = String.Format("SUBSTRING({0}, {1}, {2})", fieldName, i, 1); 
62                sql.AppendFormat("({0} BETWEEN '{1}' AND '{2}')", subStr, startWord, endWord); 
63                ++i; 
64            }
 
65
66            return sql.ToString(); 
67        }
 
68    }
 
69}