Yes and no.
ADO.NET has very good support for parameterization, and when you use it correctly, the parameter values ββwill be automatically sanitized to prevent SQL injection. Thus, you can add parameters to the SqlCommand control (or SqlDataSource ) without worrying about what is in them.
The good news is that parameterizing your material is very simple. I will show you an example of C # for programming it programmatically, but you can do it declaratively using server management tools if you want.
The bad news is that, like everything else, you still need to think about what you are doing. Any string from an unsafe source must be parameterized if you want to have any security. If you paste it verbatim into the query, you will bypass ADO.NET security features.
Secure:
string name = txtName.Text; sqlCommand.CommandText = "select * from product where name = @name"; sqlCommand.Parameters.AddWithValue("name", name);
Unprotected:
string name = txtName.Text; sqlCommand.CommandText = "select * from product where name = " + name;
If anything in your SQL query comes directly from the user, you need to put it in the parameter or all bids are disabled. And, like everything else, you can shoot in the foot if you really want to. For example, you can take the SQL code, put it in a parameter and pass it to SQL EXEC . But you would not do that, would you, because it is a very bad idea.
Still not safe (yes, I saw this in production code)!
string sql = "select * from product where name = " + txtName.Text; sqlCommand.CommandText = "exec(@sql)"; sqlCommand.Parameters.AddWithValue("sql", sql);
TL; DR: ADO.NET has great features for stopping SQL injection, but only if you use them correctly.
Justin morgan
source share