Is it safe to not parameterize an SQL query when the parameter is not a string?

前端 未结 11 833
一生所求
一生所求 2020-12-23 15:57

In terms of SQL injection, I completely understand the necessity to parameterize a string parameter; that\'s one of the oldest tricks in the book. But when can

11条回答
  •  轮回少年
    2020-12-23 16:21

    I think it's safe... technically, but it's a terrible habit to get into. Do you really want to be writing queries like this?

    var sqlCommand = new SqlCommand("SELECT * FROM People WHERE IsAlive = " + isAlive + 
    " AND FirstName = @firstName");
    
    sqlCommand.Parameters.AddWithValue("firstName", "Rob");
    

    It also leaves you vulnerable in the situation where a type changes from an integer to a string (Think employee number which, despite its name - may contain letters).

    So, we've changed the type of EmployeeNumber from int to string, but forgot to update our sql queries. Oops.

提交回复
热议问题