In terms of defending against SQL injection, for simpler queries, are one of the below strategies more effective than the other?:
Using parameterization:
using (SqlCommand command = new SqlCommand(@"SELECT * FROM @table", connection)) { command.Parameters.AddWithValue("@table", table_name); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ... } } }Using
string.Format:using (SqlCommand command = new SqlCommand(string.Format(@"SELECT * FROM {0}",table_name), connection)) using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ... } }
table_namevalues? Unsafe ones?"SELECT * FROM @table"can't be parametrised like that; that's a dynamic object. Parametrising those queries is different. You can't use a variable to replace an objects name.What exactly do you mean by "You can't use a variable to replace an objects name."?<= Try to run your 1st code example in a console app and see, you will get an SqlException.