Re: special characters in an insert statement "pbd22" <dushkin@gmail.com> wrote in message
news:515da953-e922-450f-ab12-9d05cc15cd6d@n36g2000hse.googlegroups.com...
>
> Thanks.
>
> I am using VB.NET/MySQL 5.0
>
If you use parameterised updates then your data would be automatically fixed
for you. This is available using MySqlConnector.
Otherwise you should replace certain character sequences. Heres the code I
used once (C#) which provides the SqlEscapes method to convert your data ...
private static Regex _FromToRegex = new
Regex(@"([\\\n\r\b\t'""\x1a\x00])",RegexOptions.Compiled);
public static string FromToMatchEvaluator(Match match)
{
switch (match.Value)
{
case "\\":
return "\\\\";
case "\n":
return "\\n";
case "\r":
return "\\r";
case "\b":
return "\\b";
case "\t":
return "\\t";
case "\x1a":
return "\\Z";
case "\x00":
return "\\0";
case "'":
return "\\'";
case "\"":
return "\\\"";
default:
return match.Value;
}
}
public static string SqlEscapes(string s)
{
return _FromToRegex.Replace(s,new MatchEvaluator(FromToMatchEvaluator));
}
Tigger |