Click here to Skip to main content
Sign Up to vote bad
good
See more: asp.net4
When there is a Word Such as " it's " , then it generate a error , sql syntax error , check your mysql manual .
 
we can use apostrophe two times instead of single or backslash ,but this is not solution
 
Is there any way , so anyone can directly post any word having apostrophe.
 
really need solution.
i am using vb language and mysql db.
Posted 26-Jan-13 1:28am


2 solutions

 using--
 replace(textbox.text,"'","''") solve inserting issues in mysql or sql
 
just use any control in replace function which automatically replace the single
( ' )with this ( '' )and no syntax error.Major Problem of Inserting apostrophe in Sql ,
Hence solved....
 
  Permalink  
Don't concatenate strings to build your command.
 
For example, if your code is:
MySqlCommand cmd = new MySqlCommand("INSERT INTO myTable (myColumn) VALUES('" + myTextBox.Text + "')", con);
and your text box contains
It's cold today
The the command as seen by SQL is:
INSERT INTO myTable (myColumn) VALUES('It's cold today')
which will cause an error becasue it assumes the
'It'
is the value to insert in the myColumn field, and
s cold today'
is a further part of the actual command.
 
Instead, use parametrised queries:
MySqlCommand cmd = new MySqlCommand("INSERT INTO myTable (myColumn) VALUES(@MC)", con);
cmd.Parameters.AddWithValue("@MC", myTextBox.Text);
Using this also protects you from accidental or deliberate SQL Injection attacks, which can damage or destroy your database.
  Permalink  
Comments
Mike Meinz - 26-Jan-13 7:42am
Excellent example! Shows how to handle the single quote issue, prevent SQL Injection Attacks and use parameters to improve SQL Server performance.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 6,959
1 Prasad_Kulkarni 3,671
2 OriginalGriff 3,359
3 _Amy 3,332
4 CPallini 2,925


Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid