SQL Injection in .NET

后端 未结 5 1533
自闭症患者
自闭症患者 2021-01-23 17:01

Hi I was wondering if anyone knew of some good websites detailing prevention for SQL injection for .NET web applications. Any resources would be greatly appricated, thank you.

5条回答
  •  自闭症患者
    2021-01-23 17:55

    I think that, if you google a bit on 'preventing sql injection in .NET', you'll find lots of good resources. :)

    Anyway, one very important thing, is to not use string-concatenation in order to build your queries. Instead, use parametrized queries. ADO.NET allows to do this, in a very easy way:

    string sql = "SELECT * FROM Persons WHERE Persons.Lastname LIKE @p_Name";
    
    SqlCommand cmd = new SqlCommand (sql);
    
    cmd.Parameters.Add ("@p_Name", SqlDbType.Varchar).Value = textBox1.Text + "%";
    

提交回复
热议问题