zoukankan      html  css  js  c++  java
  • 拼音查询生成SQL条件的类

    拼音查询生成SQL条件的类

    public class SQLPingYing
        

            
    private static string[] startChars = {"""""","","","","","",
                                                     
    "","","","","","","","","","""","","","","","","",""}

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


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

            public static string GetCharCondition(char cChar, string strFieldName) 
            

                
    string strWord; 
                
    int Index = (int)(char.ToUpper(cChar)) - (int)'A'
                
    if (Index >= 0 && Index < 26
                    strWord 
    = startChars[Index]; 
                
    else 
                    strWord 
    = startChars[0]; 
               
                
    //return string.Format("(({0}>='{1}' AND {0}<'[') OR ({0} >= '{3}' AND {0} < '{{') OR {0}>='{2}')",  
                
    //strFieldName, char.ToUpper(cChar), strWord, char.ToLower(cChar)); 
              
                
    return string.Format("(({0} >= '{3}' AND {0} <= 'zzzzzzzz') OR {0}>='{2}')",  
                    strFieldName, 
    char.ToUpper(cChar), strWord, char.ToLower(cChar)); 
            }
     
            
            
    /// <summary> 
            
    /// 将指定字段值的每个字符分割,这样可以生成同音查询的SQL 
            
    /// </summary> 
            
    /// <param name="fieldName">字段名</param> 
            
    /// <param name="fieldValue">字段值</param> 
            
    /// <returns>生成的可以进行同音查询的SQL</returns> 

            public static string GetCharFullCondition(string fieldName, string fieldValue) 
            

                StringBuilder sql 
    = new StringBuilder(1024); 
                
    int i = 1
                
    foreach (char c in fieldValue) 
                

                    
    if (i > 1
                        sql.Append(
    " AND "); 
                    
    int index = (int)(char.ToUpper(c)) - (int)'A'
                    
    string startWord, endWord; 
                    
    if (index >= 0 && index < 26
                    

                        startWord 
    = startChars[index]; 
                        endWord 
    = endChars[index]; 
                    }
     
                    
    else 
                    

                        startWord 
    = startChars[0]; 
                        endWord 
    = endChars[0]; 
                    }
     
                    
    string subStr = String.Format("SUBSTRING({0}, {1}, {2})", fieldName, i, 1); 
                    sql.AppendFormat(
    "({0} BETWEEN '{1}' AND '{2}')", subStr, startWord, endWord); 
                    
    ++i; 
                }
     

                
    return sql.ToString(); 
            }
     
        }
     


     


     


     

  • 相关阅读:
    LOJ DFS序
    牛客练习赛31 D神器大师泰兹瑞与威穆
    Codeforces Round #487 (Div. 2) C
    Manthan, Codefest 18 (rated, Div. 1 + Div. 2) C D
    [Windows Server 2003] 还原SQL Server数据库
    [Windows Server 2008] 查看PHP详细错误信息
    [Windows Server 2008] 安装网站伪静态
    网站Gzip压缩
    [Windows Server 2008] SQL Server 2008 数据库还原方法
    [Windows Server 2008] 安装Apache+PHP+MySQL
  • 原文地址:https://www.cnblogs.com/skyblue/p/951691.html
Copyright © 2011-2022 走看看