Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
How we can enter the special character like , "" % ? etc in sql server table using asp.net with C#?
Please Help me
Posted 15-Feb-11 3:18am
Edited 15-Feb-11 3:32am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use parametrized queries:
            using (SqlCommand com = new SqlCommand("INSERT INTO MyTable (message) " +
                                                   "VALUES (@M)", con))
                {
                com.Parameters.AddWithValue("@M", @"""%$#@?,.;");
                com.ExecuteNonQuery();
                }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I found this WITH GOOGLE ( search phrase was "t-sql storing special characters"):
 
To escape special characters in a LIKE expression you prefix them with an escape character. You get to choose which escape char to use with the ESCAPE keyword. (MSDN Ref)
 
For example this escapes the % symbol, using \ as the escape char:
 
select * from table where myfield like '%15\% off%' ESCAPE '\' 

 
If you don't know what characters will be in your string, and you don't want to treat them as wildcards, you can prefix all wildcard characters with an escape char, eg:
 
set @myString = replace(
                replace(
                replace(
                replace(@myString,'\','\\'),
                        '%','\%'),
                        '_','\_'),
                        '[','\[') 

 
(Note that you have to escape your escape char too). Then you can use something like this:
 
select * from table where myfield like '%' + @myString + '%' ESCAPE '\'

 
Also remember to allocate more space for your @myString variable as it will become longer with the string replacement.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This [^]might help you.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,192
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 15 Feb 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100