Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping values in SQL queries (C# with SQL connector)

Tags:

c#

mysql

escaping

I know I can use the parameters, but what is the right way to escape string sequences? The query could be like this:

"INSERT INTO records (ReferenceID,Name,Note,Author) VALUES ('" + ID+ "','" + addlevel.textBox1.Text + "','"+addlevel.textBox2_note.Text+ "','"+Program.Username+"')";

I am ONLY curious, just want to know :)

EDIT: But what about that? "CREATE TABLE "+string" .... parameters cannot be used here!

like image 314
Snake Avatar asked Jan 07 '10 12:01

Snake


1 Answers

If you need to perform database operations, such as creating tables, then you should use SQL Server Management Objects instead of executing SQL strings.

For CRUD operations parameters is absolutely the only true path.

UPDATE: It appears that the MySQL client library contains a helper method for this ill-advised task. You can call MySqlHelper.EscapeString(string).

like image 82
Jamie Ide Avatar answered Oct 05 '22 06:10

Jamie Ide