Basically Commands has Parameters and parameters has functions like Add, AddWithValue, and etc. In all tutorials i’ve seen, i usually noticed that they are using Add instead of AddWithValue.
.Parameters.Add("@ID", SqlDbType.Int)
vs
.Parameters.AddWithValue("@ID", 1)
Is there a reason NOT to use AddWithValue? I’d prefer to use that over
Parameters.Add("@ID", SqlDbType.Int, 4).Value = 1
since it saves my coding time. So which is better to use? Which is safe to use? Does it improves performance?
With
Add()method you may restrict user input by specifying type and length of data – especially forvarcharcolumns.In case of AddWithValue() (implicit conversion of value) method, it sends nvarchar value to the database.