Sometimes we will make some SQL in the foreground dynamically generated, this is the first thing we have to do is to verify that he is not legal, if the SQL statement at the end of and or or we have to delete processing, otherwise it will be an error.
1. Front-desk JS judge whether to meet the left and right bracket symmetry
function Checkbracket (algorithms) { var bracket = []; var algorithms = Algorithms | | ''; for (var i = 0; i < algorithms.length; i++) { if (Algorithms.charat (i) = = "(") { Bracket.push (') } if (Algorithms.charat (i) = = ")") { if (bracket.length) { bracket.pop () } else { alert (' conditional brackets are not paired, missing opening parenthesis! ') return false;}} } if (bracket.length) { alert (' conditional brackets are not paired, the closing parenthesis is missing! ') return false; } return true; }
2. Determine if the SQL statement syntax is met
public string Getbool (string exp) { string flag = "true"; Try { if (exp. EndsWith ("and") | | Exp. EndsWith ("or")) { exp = exp. Contains ("and")? Exp. Substring (0, exp. LENGTH-4): Exp. Substring (0, exp. Length-3); } EXP = exp. Replace ("and", "&&"). Replace ("or", "| |"); var type = Type.gettypefromprogid ("Msscriptcontrol.scriptcontrol"); var obj = activator.createinstance (type); Type. InvokeMember ("Language", System.Reflection.BindingFlags.SetProperty, NULL, obj, new object[] {"JavaScript"}); var result = type. InvokeMember ("Eval", System.Reflection.BindingFlags.InvokeMethod, NULL, obj, new object[] {exp}); } catch (Exception e) { flag = "false"; } return flag; }
Front desk Patchwork SQL statements to the background