insert into contract(ContractName,ContractRole,CtUpdate)
values("echo2","admin",now());update contract set ContractName='echo3',ContractRole='Web master1',CtUpdate=now(),
update=now();
where `Id`=2;delete from contract where id=2如何用正则表达式,把上面的sql语句拆分成
1,insert
string tableName = contract
string fileds = ContractName,ContractRole,CtUpdate
string values="echo2","admin",now()2, update
string tableName = contract
string fileds = ContractName,ContractRole,CtUpdate,update
string where = `Id`=2;3, delete
  string tableName = contract
string where = `Id`=2;

解决方案 »

  1.   

    1.定义一个解析类
    public class SqlAnalyze
    {
        public string tableName { get; set; }
        public string filedsList { get; set; }
        public string valuesList { get; set; }
        public string whereCondition { get; set; }}
    2.重构解析方法
     private SqlAnalyze GetSqlPart(string sql)
                {
                    SqlAnalyze sa = new SqlAnalyze();
                    if (!string.IsNullOrEmpty(sql))
                    {
                        string pattern = string.Empty;
                        Regex _reg = null;
                        Match m = null;
                        sql = sql.Trim();
                        string type = Regex.Match(sql, @"(?<=^)\S+").Value;
                        type = type.ToLower();
                        switch (type)
                        {
                            case "insert":
                                pattern = @"(?i)into\s+?([^(\s]+?)\(([^)]+)\)[\s\S]*?values\s*?\(([^)]+)\)";
                                _reg = new Regex(pattern);
                                m = _reg.Match(sql);
                                sa.tableName = m.Groups[1].Value;
                                sa.filedsList = m.Groups[2].Value;
                                sa.valuesList = m.Groups[3].Value;
                                break;
                            case "update":
                                pattern = @"(?i)update\s+?(\S+?)\s+?set\s+((([^=]+?)=[^\s,]+?,?)+)\s+?where\s+?(?<where>[\s\S]*)";
                                _reg = new Regex(pattern);
                                m = _reg.Match(sql);
                                sa.tableName = m.Groups[1].Value;
                                sa.filedsList = string.Join(",",Regex.Matches(m.Groups[2].Value,@"[^='\s,]+?(?==)").Cast<Match>().Select(a=>a.Value).ToList());
                                sa.whereCondition = m.Groups["where"].Value;
                                break;
                            case "delete":
                                pattern = @"(?i)delete\s+?from\s+?(\S+?)\s+?where\s+?([\s\S]*)";
                                _reg = new Regex(pattern);
                                m = _reg.Match(sql);
                                sa.tableName = m.Groups[1].Value;
                                sa.whereCondition = sa.whereCondition = m.Groups[2].Value;
                                break;
                        }
                    }
                    return sa;
                }3.调用
    SqlAnalyze sa = GetSqlPart(@"update contract set ContractName='echo3',ContractRole='Web master1',CtUpdate=now(),
                                                update=now()
                                                where Id=2");
                    /*
                     - sa {SqlAnalyze} SqlAnalyze
                        filedsList "ContractName,ContractRole,CtUpdate,update" string
                        tableName "contract" string
                        valuesList null string
                        whereCondition "Id=2" string                 */
                    sa = GetSqlPart(@"insert into contract(ContractName,ContractRole,CtUpdate)
                                    values(""echo2"",""admin"",now())");
                    /*
                      - sa {SqlAnalyze} SqlAnalyze
                            filedsList "ContractName,ContractRole,CtUpdate" string
                            tableName "contract" string
                            valuesList "\"echo2\",\"admin\",now(" string
                            whereCondition null string                 */
                    sa = GetSqlPart(@"delete from contract where id=2");
                    /*
                      - sa {SqlAnalyze} SqlAnalyze
                        filedsList null string
                        tableName "contract" string
                        valuesList null string
                        whereCondition "id=2" string                 */
      

  2.   


    to:Return_false
    .Cast<Match>这个编译不过啊,要参照什么程序集啊啊啊啊?