In Asp.net, we strongly recommend that you use parameters to implement SQL statements instead of SQL concatenation, because even if you filter hundreds of passwords
For example:
Sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. deleettings ["conn"]);
Sqlcommand comm = new sqlcommand ("Update tb1 set vname = @ vname, iage = @ iage where id = @ ID", Conn );
Sqlparameter parm1 = new sqlparameter ("@ vname", sqldbtype. nvarchar, 50 );
Parm1.value = (textbox) E. Item. findcontrol ("name"). text;
Sqlparameter parm2 = new sqlparameter ("@ iage", sqldbtype. INT );
Parm2.value = (textbox) E. Item. findcontrol ("Age"). text;
Sqlparameter parm3 = new sqlparameter ("@ ID", sqldbtype. INT );
Parm3.value = This. datagrid1.datakeys [E. Item. itemindex];
Comm. Parameters. Add (parm1 );
Comm. Parameters. Add (parm2 );
Comm. Parameters. Add (parm3 );
Conn. open ();
Comm. executenonquery ();
Conn. Close ();
This code looks comfortable and safe. Why?
The so-called SQL injection attack means that an attacker inserts an SQL command into the input field of a web form or the query string requested by the page, and deceives the server to execute malicious SQL commands. In some forms, the content entered by users is directly used to construct (or affect) dynamic SQL commands or as input parameters of stored procedures. Such forms are particularly vulnerable to SQL injection attacks. Common SQL injection attacks include:
(1) An ASP. NET web application has a logon page that controls whether the user has the right to access the application. It requires the user to enter a name and password.
(2) The content entered on the logon page is directly used to construct dynamic SQL commands, or directly used as parameters of stored procedures. The following is an example of constructing a query for an ASP. NET application:
System. Text. stringbuilder query = new system. Text. stringbuilder (
Select * from users where login =)
. Append (txtlogin. Text). append (and Password =)
. Append (txtpassword. Text). append ();