| |

VerySource

 Forgot password?
 Register
Search
View: 1186|Reply: 12

How to use SQL statements to prevent injection attacks?

[Copy link]

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-2-8 09:00:02
| Show all posts |Read mode
I heard that you can use sqlcommand Parameters to prevent injection attacks
I wrote the following statement:
string sql = "select * from members where userName = @ name";
OleDbCommand cmd = new OleDbCommand (sql, con);
con.Open ();
 // Prevent injection attacks
cmd.Parameters.Add ("@ name", OleDbType.VarChar, 50);
cmd.Parameters ["@ name"]. Value = this.TextBox1.Text;
If I now type "'" (comma) in textbox1, what is the value of sql?
I do n’t understand, that big brother helped write about it ~
Let's say I want to change "'" (comma) to "" (space)
Reply

Use magic Report

1

Threads

31

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-3-30 14:00:01
| Show all posts
This can be prevented with stored procedures.
Reply

Use magic Report

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-3-30 16:45:01
| Show all posts
Can you be more detailed? explain
Reply

Use magic Report

1

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-30 19:30:01
| Show all posts
Using stored procedures
Reply

Use magic Report

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-3-30 23:30:02
| Show all posts
How to use it?
For example, I want to turn commas into spaces?
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-4-1 10:00:01
| Show all posts
Use parameters, don't spell the values.
Some problems cannot be solved using stored procedures.
Reply

Use magic Report

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-4-1 19:15:02
| Show all posts
To use parameters is to use the statement I just wrote? But I didn't find any conversion during debugging?
How to write the correct sentence?
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-4-2 13:00:02
| Show all posts
If you enter ",", the generated SQL statement is equivalent to:
"select * from members where userName = ','"
If you want to replace, you can
cmd.Parameters ["@ name"]. Value = this.TextBox1.Text.Replace (",", "") `;
Reply

Use magic Report

2

Threads

11

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-4-4 16:45:01
| Show all posts
Still not good, I wrote it like this
 OleDbConnection con = new OleDbConnection ("server = hp; uid = sa; pwd = sa; database = test; Provider = SQLOLEDB");
           string sql = "select * from members where userName = @ userName";
            OleDbCommand cmd = new OleDbCommand (sql, con);
           con.Open ();
           // Prevent injection attacks
           cmd.Parameters.Add ("@ userName", OleDbType.VarChar, 50);
           cmd.Parameters ["@ userName"]. Value = this.TextBox1.Text.Replace ("'", "");
           The value of sql is still select * from members where userName = @ userName
Where is wrong? Urgent ~
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-4 19:30:01
| Show all posts
Use parameters, do not use string dynamic splicing into SQL statements, generally no problem
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list