Consider the following code:
Dim sql = "SELECT * FROM MyTable WHERE value1 = @Param1"
If someCondition Then
sql = sql + " AND value2 = @Param2"
End If
Dim cmd As New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@Param1", param1Value)
cmd.Parameters.AddWithValue("@Param2", param2Value)
Assuming I built a complex sql statement dynamically that may or may not include a parameter @Param2- is there any harm in adding it to the command as a parameter?
My real use case is obviously much more complicated than that, but overall this is a pattern that I should avoid; and if so, why?
source
share