Codificar datos con EncodeHTML Query

Codificar datos para insertar en campos de SQL.

public string encodeHTML(string str)
        {
            string regExp = "\\";
            str = str.Replace(regExp, "\");

            regExp = "\"";
            str = str.Replace(regExp, """);

            regExp = " '";
            str = str.Replace(regExp, "'");

            regExp = "<";
            str = str.Replace(regExp, "&lt;");

            regExp = ">";
            str = str.Replace(regExp, "&gt;");

            regExp = " ";
            str = str.Replace(regExp, "&nbsp;");

            regExp = "¡";
            str = str.Replace(regExp, "&iexcl;");

            regExp = "¢";
            str = str.Replace(regExp, "&cent;");

            regExp = "£";
            str = str.Replace(regExp, "&pound;");

            regExp = "¤";
            str = str.Replace(regExp, "&curren;");

            regExp = "¥";
            str = str.Replace(regExp, "&yen;");

            regExp = "¦";
            str = str.Replace(regExp, "&brvbar;");

            regExp = "§";
            str = str.Replace(regExp, "&sect;");

            regExp = "¨";
            str = str.Replace(regExp, "&uml;");

            regExp = "©";
            str = str.Replace(regExp, "&copy;");

            regExp = " ª ";
            str = str.Replace(regExp, "&ordf;");

            regExp = "«";
            str = str.Replace(regExp, "&laquo;");

            regExp = "¬";
            str = str.Replace(regExp, "&not;");


            regExp = "®";
            str = str.Replace(regExp, "&reg;");

            regExp = "¯";
            str = str.Replace(regExp, "&macr;");

            regExp = "°";
            str = str.Replace(regExp, "&deg;");

            regExp = "±";
            str = str.Replace(regExp, "&#177;");

            regExp = "²";
            str = str.Replace(regExp, "&sup2;");

            regExp = "³";
            str = str.Replace(regExp, "&sup3;");

            regExp = "´";
            str = str.Replace(regExp, "&acute;");

            regExp = " µ ";
            str = str.Replace(regExp, "&micro;");

            regExp = "¶";
            str = str.Replace(regExp, "&para;");

            regExp = "·";
            str = str.Replace(regExp, "&middot;");

            regExp = "¸";
            str = str.Replace(regExp, "&cedil;");

            regExp = "¹";
            str = str.Replace(regExp, "&sup1;");

            regExp = " º ";
            str = str.Replace(regExp, "&ordm;");

            regExp = "»";
            str = str.Replace(regExp, "&raquo;");

            regExp = "¼";
            str = str.Replace(regExp, "&frac14;");

            regExp = "½";
            str = str.Replace(regExp, "&frac12;");

            regExp = "¾";
            str = str.Replace(regExp, "&frac34;");

            regExp = "¿";
            str = str.Replace(regExp, "&iquest;");

            regExp = "×";
            str = str.Replace(regExp, "&times;");

            regExp = "÷";
            str = str.Replace(regExp, "&divide;");

            regExp = " À ";
            str = str.Replace(regExp, "&Agrave;");

            regExp = " Á ";
            str = str.Replace(regExp, "&Aacute;");

            regExp = " Â ";
            str = str.Replace(regExp, "&Acirc;");

            regExp = " Ã ";
            str = str.Replace(regExp, "&Atilde;");

            regExp = " Ä ";
            str = str.Replace(regExp, "&Auml;");

            regExp = " Å ";
            str = str.Replace(regExp, "&Aring;");

            regExp = " Æ ";
            str = str.Replace(regExp, "&AElig;");

            regExp = " Ç ";
            str = str.Replace(regExp, "&Ccedil;");

            regExp = " È ";
            str = str.Replace(regExp, "&Egrave;");

            regExp = " É ";
            str = str.Replace(regExp, "&Eacute;");

            regExp = " Ê ";
            str = str.Replace(regExp, "&Ecirc;");

            regExp = " Ë ";
            str = str.Replace(regExp, "&Euml;");

            regExp = " Ì ";
            str = str.Replace(regExp, "&Igrave;");

            regExp = " Í ";
            str = str.Replace(regExp, "&Iacute;");

            regExp = " Î ";
            str = str.Replace(regExp, "&Icirc;");

            regExp = " Ï ";
            str = str.Replace(regExp, "&Iuml;");

            regExp = " Ð ";
            str = str.Replace(regExp, "&ETH;");

            regExp = " Ñ ";
            str = str.Replace(regExp, "&Ntilde;");

            regExp = " Ò ";
            str = str.Replace(regExp, "&Ograve;");

            regExp = " Ó ";
            str = str.Replace(regExp, "&Oacute;");

            regExp = " Ô ";
            str = str.Replace(regExp, "&Ocirc;");

            regExp = " Õ ";
            str = str.Replace(regExp, "&Otilde;");

            regExp = " Ö ";
            str = str.Replace(regExp, "&Ouml;");

            regExp = " Ø ";
            str = str.Replace(regExp, "&Oslash;");

            regExp = " Ù ";
            str = str.Replace(regExp, "&Ugrave;");

            regExp = " Ú ";
            str = str.Replace(regExp, "&Uacute;");

            regExp = " Û ";
            str = str.Replace(regExp, "&Ucirc;");

            regExp = " Ü ";
            str = str.Replace(regExp, "&Uuml;");

            regExp = " Ý ";
            str = str.Replace(regExp, "&Yacute;");

            regExp = " Þ ";
            str = str.Replace(regExp, "&THORN;");

            regExp = " ß ";
            str = str.Replace(regExp, "&szlig;");

            regExp = " à ";
            str = str.Replace(regExp, "&agrave;");

            regExp = " á ";
            str = str.Replace(regExp, "&aacute;");

            regExp = " â ";
            str = str.Replace(regExp, "&acirc;");

            regExp = " ã ";
            str = str.Replace(regExp, "&atilde;");

            regExp = " ä ";
            str = str.Replace(regExp, "&auml;");

            regExp = " å ";
            str = str.Replace(regExp, "&aring;");

            regExp = " æ ";
            str = str.Replace(regExp, "&aelig;");

            regExp = " ç ";
            str = str.Replace(regExp, "&ccedil;");

            regExp = " è ";
            str = str.Replace(regExp, "&egrave;");

            regExp = " é ";
            str = str.Replace(regExp, "&eacute;");

            regExp = " ê ";
            str = str.Replace(regExp, "&ecirc;");

            regExp = " ë ";
            str = str.Replace(regExp, "&euml;");

            regExp = " ì ";
            str = str.Replace(regExp, "&igrave;");

            regExp = " í ";
            str = str.Replace(regExp, "&iacute;");

            regExp = " î ";
            str = str.Replace(regExp, "&icirc;");

            regExp = " ï ";
            str = str.Replace(regExp, "&iuml;");

            regExp = " ð ";
            str = str.Replace(regExp, "&eth;");

            regExp = " ñ ";
            str = str.Replace(regExp, "&ntilde;");

            regExp = "ò";
            str = str.Replace(regExp, "&ograve;");

            regExp = "ó";
            str = str.Replace(regExp, "&oacute;");

            regExp = "ô";
            str = str.Replace(regExp, "&ocirc;");

            regExp = "õ";
            str = str.Replace(regExp, "&otilde;");

            regExp = "ö";
            str = str.Replace(regExp, "&ouml;");

            regExp = "ø";
            str = str.Replace(regExp, "&oslash;");

            regExp = "ù";
            str = str.Replace(regExp, "&ugrave;");

            regExp = "ú";
            str = str.Replace(regExp, "&uacute;");

            regExp = "û";
            str = str.Replace(regExp, "&ucirc;");

            regExp = "ü";
            str = str.Replace(regExp, "&uuml;");

            regExp = "ý";
            str = str.Replace(regExp, "&yacute;");

            regExp = "þ";
            str = str.Replace(regExp, "&thorn;");

            regExp = "ÿ";
            str = str.Replace(regExp, "&yuml;");

            regExp = "∀";
            str = str.Replace(regExp, "&forall;");

            regExp = "∂";
            str = str.Replace(regExp, "&part;");

            regExp = "∃";
            str = str.Replace(regExp, "&exist;");

            regExp = "∅";
            str = str.Replace(regExp, "&empty;");

            regExp = "∇";
            str = str.Replace(regExp, "&nabla;");

            regExp = "∈";
            str = str.Replace(regExp, "&isin;");

            regExp = "∉";
            str = str.Replace(regExp, "&notin;");

            regExp = "∋";
            str = str.Replace(regExp, "&ni;");

            regExp = "∏";
            str = str.Replace(regExp, "&prod;");

            regExp = "∑";
            str = str.Replace(regExp, "&sum;");

            regExp = "−";
            str = str.Replace(regExp, "&minus;");

            regExp = "∗";
            str = str.Replace(regExp, "&lowast;");

            regExp = "√";
            str = str.Replace(regExp, "&radic;");

            regExp = "∝";
            str = str.Replace(regExp, "&prop;");

            regExp = "∞";
            str = str.Replace(regExp, "&infin;");

            regExp = "∠";
            str = str.Replace(regExp, "&ang;");

            regExp = "∧";
            str = str.Replace(regExp, "&and;");

            regExp = "∨";
            str = str.Replace(regExp, "&or;");

            regExp = "∩";
            str = str.Replace(regExp, "&cap;");

            regExp = "∪";
            str = str.Replace(regExp, "&cup;");

            regExp = "∫";
            str = str.Replace(regExp, "&int;");

            regExp = "∴";
            str = str.Replace(regExp, "&there4;");

            regExp = "∼";
            str = str.Replace(regExp, "&sim;");

            regExp = "≅";
            str = str.Replace(regExp, "&cong;");

            regExp = "≈";
            str = str.Replace(regExp, "&asymp;");

            regExp = "≠";
            str = str.Replace(regExp, "&ne;");

            regExp = "≡";
            str = str.Replace(regExp, "&equiv;");

            regExp = "≤";
            str = str.Replace(regExp, "&le;");

            regExp = "≥";
            str = str.Replace(regExp, "&ge;");

            regExp = "⊂";
            str = str.Replace(regExp, "&sub;");

            regExp = "⊃";
            str = str.Replace(regExp, "&sup;");

            regExp = "⊄";
            str = str.Replace(regExp, "&nsub;");

            regExp = "⊆";
            str = str.Replace(regExp, "&sube;");

            regExp = "⊇";
            str = str.Replace(regExp, "&supe;");

            regExp = "⊕";
            str = str.Replace(regExp, "&oplus;");

            regExp = "⊗";
            str = str.Replace(regExp, "&otimes;");

            regExp = "⊥";
            str = str.Replace(regExp, "&perp;");

            regExp = "⋅";
            str = str.Replace(regExp, "&sdot;");

            regExp = "Α";
            str = str.Replace(regExp, "&Alpha;");

            regExp = "Β";
            str = str.Replace(regExp, "&Beta;");

            regExp = "Γ";
            str = str.Replace(regExp, "&Gamma;");

            regExp = "Δ";
            str = str.Replace(regExp, "&Delta;");

            regExp = "Ε";
            str = str.Replace(regExp, "&Epsilon;");

            regExp = "Ζ";
            str = str.Replace(regExp, "&Zeta;");

            regExp = "Η";
            str = str.Replace(regExp, "&Eta;");

            regExp = "Θ";
            str = str.Replace(regExp, "&Theta;");

            regExp = "Ι";
            str = str.Replace(regExp, "&Iota;");

            regExp = "Κ";
            str = str.Replace(regExp, "&Kappa;");

            regExp = "Λ";
            str = str.Replace(regExp, "&Lambda;");

            regExp = "Μ";
            str = str.Replace(regExp, "&Mu;");

            regExp = "Ν";
            str = str.Replace(regExp, "&Nu;");

            regExp = "Ξ";
            str = str.Replace(regExp, "&Xi;");

            regExp = "Ο";
            str = str.Replace(regExp, "&Omicron;");

            regExp = "Π";
            str = str.Replace(regExp, "&Pi;");

            regExp = "Ρ";
            str = str.Replace(regExp, "&Rho;");

            regExp = "Σ";
            str = str.Replace(regExp, "&Sigma;");

            regExp = "Τ";
            str = str.Replace(regExp, "&Tau;");

            regExp = "Υ";
            str = str.Replace(regExp, "&Upsilon;");

            regExp = "Φ";
            str = str.Replace(regExp, "&Phi;");

            regExp = "Χ";
            str = str.Replace(regExp, "&Chi;");

            regExp = "Ψ";
            str = str.Replace(regExp, "&Psi;");

            regExp = "Ω";
            str = str.Replace(regExp, "&Omega;");

            regExp = "α";
            str = str.Replace(regExp, "&alpha;");

            regExp = "β";
            str = str.Replace(regExp, "&beta;");

            regExp = "γ";
            str = str.Replace(regExp, "&gamma;");

            regExp = "δ";
            str = str.Replace(regExp, "&delta;");

            regExp = "ε";
            str = str.Replace(regExp, "&epsilon;");

            regExp = "ζ";
            str = str.Replace(regExp, "&zeta;");

            regExp = "η";
            str = str.Replace(regExp, "&eta;");

            regExp = "θ";
            str = str.Replace(regExp, "&theta;");

            regExp = "ι";
            str = str.Replace(regExp, "&iota;");

            regExp = "κ";
            str = str.Replace(regExp, "&kappa;");

            regExp = "λ";
            str = str.Replace(regExp, "&lambda;");

            regExp = "μ";
            str = str.Replace(regExp, "&mu;");

            regExp = "ν";
            str = str.Replace(regExp, "&nu;");

            regExp = "ξ";
            str = str.Replace(regExp, "&xi;");

            regExp = "ο";
            str = str.Replace(regExp, "&omicron;");

            regExp = "π";
            str = str.Replace(regExp, "&pi;");

            regExp = "ρ";
            str = str.Replace(regExp, "&rho;");

            regExp = "ς";
            str = str.Replace(regExp, "&sigmaf;");

            regExp = "σ";
            str = str.Replace(regExp, "&sigma;");

            regExp = "τ";
            str = str.Replace(regExp, "&tau;");

            regExp = "υ";
            str = str.Replace(regExp, "&upsilon;");

            regExp = "φ";
            str = str.Replace(regExp, "&phi;");

            regExp = "χ";
            str = str.Replace(regExp, "&chi;");

            regExp = "ψ";
            str = str.Replace(regExp, "&psi;");

            regExp = "ω";
            str = str.Replace(regExp, "&omega;");

            regExp = "ϑ";
            str = str.Replace(regExp, "&thetasym;");

            regExp = "ϒ";
            str = str.Replace(regExp, "&upsih;");

            regExp = "ϖ";
            str = str.Replace(regExp, "&piv;");

            regExp = "Œ";
            str = str.Replace(regExp, "&OElig;");

            regExp = "œ";
            str = str.Replace(regExp, "&oelig;");

            regExp = "Š";
            str = str.Replace(regExp, "&Scaron;");

            regExp = "š";
            str = str.Replace(regExp, "&scaron;");

            regExp = "Ÿ";
            str = str.Replace(regExp, "&Yuml;");

            regExp = "ƒ";
            str = str.Replace(regExp, "&fnof;");

            regExp = "ˆ";
            str = str.Replace(regExp, "&circ;");

            regExp = "˜";
            str = str.Replace(regExp, "&tilde;");

            regExp = " ";
            str = str.Replace(regExp, "&ensp;");

            regExp = " ";
            str = str.Replace(regExp, "&emsp;");

            regExp = " ";
            str = str.Replace(regExp, "&thinsp;");

            regExp = "	‎";
            str = str.Replace(regExp, "&lrm;");

            regExp = "–";
            str = str.Replace(regExp, "&ndash;");

            regExp = "—";
            str = str.Replace(regExp, "&mdash;");

            regExp = "‘";
            str = str.Replace(regExp, "&lsquo;");

            regExp = "’";
            str = str.Replace(regExp, "&rsquo;");

            regExp = "‚";
            str = str.Replace(regExp, "&sbquo;");

            regExp = "“";
            str = str.Replace(regExp, "&ldquo;");

            regExp = "”";
            str = str.Replace(regExp, "&rdquo;");

            regExp = "„";
            str = str.Replace(regExp, "&bdquo;");

            regExp = "†";
            str = str.Replace(regExp, "&dagger;");

            regExp = "‡";
            str = str.Replace(regExp, "&Dagger;");

            regExp = "•";
            str = str.Replace(regExp, "&bull;");

            regExp = "…";
            str = str.Replace(regExp, "&hellip;");

            regExp = "‰";
            str = str.Replace(regExp, "&permil;");

            regExp = "′";
            str = str.Replace(regExp, "&prime;");

            regExp = "″";
            str = str.Replace(regExp, "&Prime;");

            regExp = "‹";
            str = str.Replace(regExp, "&lsaquo;");

            regExp = "›";
            str = str.Replace(regExp, "&rsaquo;");

            regExp = "‾";
            str = str.Replace(regExp, "&oline;");

            regExp = "€";
            str = str.Replace(regExp, "&euro;");

            regExp = "™";
            str = str.Replace(regExp, "&trade;");

            regExp = "←";
            str = str.Replace(regExp, "&larr;");

            regExp = "↑";
            str = str.Replace(regExp, "&uarr;");

            regExp = "→";
            str = str.Replace(regExp, "&rarr;");

            regExp = "↓";
            str = str.Replace(regExp, "&darr;");

            regExp = "↔";
            str = str.Replace(regExp, "&harr;");

            regExp = "↵";
            str = str.Replace(regExp, "&crarr;");

            regExp = "⌈";
            str = str.Replace(regExp, "&lceil;");

            regExp = "⌉";
            str = str.Replace(regExp, "&rceil;");

            regExp = "⌊";
            str = str.Replace(regExp, "&lfloor;");

            regExp = "⌋";
            str = str.Replace(regExp, "&rfloor;");

            regExp = "◊";
            str = str.Replace(regExp, "&loz;");

            regExp = "♠";
            str = str.Replace(regExp, "&spades;");

            regExp = "♣";
            str = str.Replace(regExp, "&clubs;");

            regExp = "♥";
            str = str.Replace(regExp, "&hearts;");

            regExp = "♦";
            str = str.Replace(regExp, "&diams;");

            return str;
        }

Con esta funcion se puede guardar un query dentro de un campo o codigos especiales en una base de datos sin que generen conflictos en un INSERT o UPDATE

Codificar datos con EncodeHTML Query
Web | + posts

Full Stack Software Developer.

Fundador de Kitaokatech y creador del ERP Hanbai.

Scroll hacia arriba