I faced with problem filtering string when i insert data into my table. I mean special characters like '
<
etc i can do simpl开发者_如何转开发e replace but i need to save the original sequence of characters. Thanks for help in advance.
If you're looking to change the string through code you could specify an array of characters to strip from the string. The String.Join can use the split method to remove those characters and put it back together
string s = "He<llo;< I -am a $stri-ng;";
// Add other characters that you deem unacceptable
char[] invalidChars = { ';', '<', '>', '-' };
s = String.Join("", s.Split(invalidChars));
In terms of saving the original sequence, this is very vague so I'm not sure what you mean but what pops into my mind is to use the database and create a second column so that you have one for what they entered in, and another for the fixed value. Two ways pop into my head to control this
Database Trigger - Design some function to strip away characters and save to the fixed value column. The risk in this is that if someone decides to key a character you have not trapped yet you would not only modify the function but have to run this function against any record that slipped by
Computed Column Specification - Create an expression based on the function to strip off characters and let the column control this value This is sufficient enough to defeat the risk added through the trigger. When you modify the function since this is a computed column it would in theory update the value. (Correct me if I am wrong here, not tested, just in theory)
Probably not the most efficient approaches but this is what I would use with my current toolset.
精彩评论