Deleting weird characters
- Hello programmers,
Im having a problem with reading an XML into a MySQL database
Every record goes good, but when i have this character: ′ Then it crashes so
i added this one to my list but still doesn't delete the weird character..
valueAttribute = valueAttribute.Replace("'", "\\'"); valueAttribute = valueAttribute.Replace("’", "\\'"); valueAttribute = valueAttribute.Replace("′", "\\'"); valueAttribute = valueAttribute.Replace("‘", "\\'"); valueAttribute = valueAttribute.Replace("′", "\\'"); valueAttribute = valueAttribute.Replace("′", "");
Risposte
- Hello
Since I am not fully aware of the scenario in which you are trying to replace the single-quote(') character.But if it is related to some database related operation then preferably you should try to use perameterized queries to handle such things.
Well also I am thinking why in your code you are not able to replace the single quote(').Following statement works fine for me :-
string strReplace = @"MyString'";
Console.WriteLine(strReplace.Replace("'",@"\")); --> MyString\
Please let me know if I have missed something.Also Char Code for single quote is 39.So run your statement with 39 as the char code.- Proposto come rispostaChao KuoMSFT, Moderatormartedì 10 novembre 2009 7.27
- Contrassegnato come rispostaChao KuoMSFT, Moderatorvenerdì 13 novembre 2009 3.54
- Hi,
You need to replace ' with two ', i.e
valueAttribute = valueAttribute.Replace("'", "''");
as suggested already by raihansazal, however there may be other characters or issues that can mess up your code and make it vulnerable to sql injection attacks. To avoid all these issues you should use parameterised queries as suggested by Pawan Mishra... it's the safest and best way.
The \' isn't working because that's how you 'escape' or 'encode' characters for embedding in a string in C# and c like languages, for sql the standard mechansim is to repeat the character, so ' becomes '' instead.- Proposto come rispostaChao KuoMSFT, Moderatormartedì 10 novembre 2009 7.29
- Contrassegnato come rispostaChao KuoMSFT, Moderatorvenerdì 13 novembre 2009 3.54
Tutte le risposte
- Hi,
Try to find the char code of that character with valueAttribute.ToCharArray() method.
Then you can use the following code.
valueAttribute = valueAttribute.Replace(Convert.ToChar(60).ToString(), "'");//here 60 is the code of the character.
- How can i know that ′ is char(60)?
As I mentioned above you can use valueAttribute.ToCharArray() at debug while breakpoint is hit.
Then for example if the character is 5th in your string typevalueAttribute.ToCharArray()[5] as expression to quickwatch window. And quickwatch window will show you the number you should use.- Im just a starter in this might sound stupid these questions but i really want to learn.
How can i let those code show in my DOS window?
Console.WriteLine(Description.ToCharArray()); // This doesnt show every character - You can use the following code.
foreach (char c in Description.ToCharArray()) { Console.WriteLine(c + ":" + Convert.ToInt32(c)); } - Lookin good =D
Character : 8242
valueAttribute = valueAttribute.Replace( Convert .ToChar(8242).ToString(), ' \\' );
But still doesnt replace that character :(
- Hello
Since I am not fully aware of the scenario in which you are trying to replace the single-quote(') character.But if it is related to some database related operation then preferably you should try to use perameterized queries to handle such things.
Well also I am thinking why in your code you are not able to replace the single quote(').Following statement works fine for me :-
string strReplace = @"MyString'";
Console.WriteLine(strReplace.Replace("'",@"\")); --> MyString\
Please let me know if I have missed something.Also Char Code for single quote is 39.So run your statement with 39 as the char code.- Proposto come rispostaChao KuoMSFT, Moderatormartedì 10 novembre 2009 7.27
- Contrassegnato come rispostaChao KuoMSFT, Moderatorvenerdì 13 novembre 2009 3.54
- you can use:
stingObj = stringObj..Replace("'", "''"); - Hi,
You need to replace ' with two ', i.e
valueAttribute = valueAttribute.Replace("'", "''");
as suggested already by raihansazal, however there may be other characters or issues that can mess up your code and make it vulnerable to sql injection attacks. To avoid all these issues you should use parameterised queries as suggested by Pawan Mishra... it's the safest and best way.
The \' isn't working because that's how you 'escape' or 'encode' characters for embedding in a string in C# and c like languages, for sql the standard mechansim is to repeat the character, so ' becomes '' instead.- Proposto come rispostaChao KuoMSFT, Moderatormartedì 10 novembre 2009 7.29
- Contrassegnato come rispostaChao KuoMSFT, Moderatorvenerdì 13 novembre 2009 3.54

