WiredWorx || Web design || SEO || Bournemouth

Web design, website development and search engine optimisation (SEO) in Bournemouth, Dorset, UK.

Save apostrophe to a MYSQL database using c#

Comments (0)

This is quite obvious, but I thought I'd post in anyhow!

On testing of a site the save kept failing on the create. After a bit of head scratching and staring at SQL it was because I had failed to validate the strings, so there were still apostrophes in there, MYSQL was thinking it was a string terminator and therefore getting it's knickers in a bit of a twist! To solve this I found this article and modified it to suit my purposes:

private string escapeChar(string strToEsc)
{
if (strToEsc.IndexOf("'") > -1)
{
strToEsc = strToEsc.Replace("'", @"'");
}
if (strToEsc.IndexOf("’") > -1)
{
strToEsc = strToEsc.Replace("’", @"’");
}
if (strToEsc.IndexOf("‘") > -1)
{
strToEsc = strToEsc.Replace("‘", @"‘");
}
return strToEsc;
}

And voila!

Comments

No commenting allowed at this time.

No commenting allowed at this time.