string text = "SELECT * from [admin] where aa=1 and cc= ' B ' ORDER by AA Desc";
Regex reg = null; reg = new Regex (@ "\s+from\s+.*?" ( \s+where\s+|\s+order\s+|\s+group\s+) |\s+from\s+.+ ", regexoptions.ignorecase);
string table = Reg. Match (text). Value; Table = Regex.Replace (table. ToLower (), @ "\s+from\s+|\s+where\s+|\[|\]|\s+order\s+|\s+group\s+", "" ");
reg = new Regex (@ "select\s+.*?\s+from\s+", regexoptions.ignorecase); String field = Reg. Match (text). Value;
field = Regex.Replace (field. ToLower (), @ "select\s+|\s+from\s+|\[|\]", "" "); reg = new Regex (@ "\s+where\s+.*?" ( \s+order\s+|\s+group\s+) |\s+where\s+.+ ", regexoptions.ignorecase); String condition = Reg. Match (text). Value; Condition = regex.replace (condition. ToLower (), @ "\s+where\s+|\s+order\s+|\s+group\s+|\[|\]", "" "); reg = new Regex (@ "\s+order\s+by\s+.*?\s (DESC|ASC) |\s+order\s+by\s+.*?\s", regexoptions.ignorecase); String order = Reg. Match (text). Value; Order = Regex.Replace (order. ToLower (), @ "\s+order\s+by\s+|\[|\]", "" ");
Use regular expressions to get SQL query statement items (table name, field, condition, sort)