Click here to Skip to main content
15,905,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to make sql query and i want to add single quotes to query but it don't work :(


C#
string quarry = "Select * from essayT where (EsId=EsId)  ";
            if (TextBox1.Text != "")
            {
                string strReplace = TextBox1.Text.Replace(' ', '%');
                quarry = quarry + "And ( EsTittleF like N'%" + strReplace + "%')";
    
            }


in this example i try to get this query

Select * from essayT where (EsId=EsId) And (EsTittlef like '%textbox1.text%' )

i cant insert single quotes to string
plz help tnx guys:)
Posted
Updated 24-Apr-14 1:43am
v4
Comments
[no name] 24-Apr-14 7:22am    
http://stackoverflow.com/questions/254009/in-c-add-quotes-around-string-in-a-comma-delimited-list-of-strings
[no name] 24-Apr-14 7:23am    
http://www.codeproject.com/Questions/456183/APPEND-SINGLE-QUOTE-WITH-VALUES-IN-STRING-BUILDER

Don't try to do it like that: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
C#
string strSelect = @"SELECT * FROM essayT WHERE EsTittleF LIKE '%' + @SS + '%'";
using (SqlCommand cmd = new SqlCommand(strSelect, con))
    {
    cmd.Parameters.AddWithValue("@SS", Textbox1.Text);
 
Share this answer
 
Comments
farham_heidari 24-Apr-14 7:42am    
i am checking more than one textbox so i realy don't know which of them will be fill by user ... i can't use sql parameters
OriginalGriff 24-Apr-14 7:47am    
"i can't use sql parameters"
Why not? It's the same thing as using the textbox content.
Plus, if you don't use SQL parameters, then along comes me (or your best mate) and deletes your database by typing in the textbox...
farham_heidari 24-Apr-14 11:00am    
i am a littler confused ... plz help me
i have a class name db for select and link to my data base the syntax is
DataTable dtessay = db.Select("Select * from essayT where (EsId=EsId)", CommandType.Text);

i want to make a search in essay table how can i generate a good query ????????
OriginalGriff 24-Apr-14 11:18am    
Why is your WHERE clause like that? It's the same as not having a WHERE at all...since the EsId column will always match the value in the EsId column... :laugh:

Doing it like that is dangerous. Seriously dangerous. When you build an SQL query by including a TextBox in the SELECT string:
string s = "Select * from essayT where (EsId=EsId) And ( EsTittleF like N'%" + TextBox1.Text + "%')";";
You hand over control of your db to anyone you can type in the text box.
Back up your database and enter this in the textbox:
x');DROP TABLE essayT;--
When you run your query, the SQL that is passed through is:
Select * from essayT where (EsId=EsId) And (EsTittleF like N'%x');DROP TABLE essayT;--%')
Which SQL sees as three separate commands:
A SELECT for rows that end with "x"
A delete command for your table
A comment
So it does each of those in sequence. This is called SQL Injection and it is no joke: people do do this just to see what will happen.

If you do not change your code to use parameterised queries, then at some point, someone will try it, and it will work...

So change your db class to let you use parameters. If you don't, you had better get very, very good at doing backups. Because you *are* going to need them.
see the following code
string strReplace = TextBox1.Text.Replace("'", "%");
 
Share this answer
 
Try changing below line

quarry = quarry + "And ( EsTittleF like N%'" + strReplace + "%')";


with this

quarry = quarry + "And ( EsTittleF like N'%" + strReplace + "%')";
 
Share this answer
 
Comments
farham_heidari 24-Apr-14 7:41am    
it didn't work :(
 
Share this answer
 
Comments
[no name] 24-Apr-14 7:24am    
http://www.codeproject.com/Questions/456183/APPEND-SINGLE-QUOTE-WITH-VALUES-IN-STRING-BUILDER

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900