I have always included the at sign in the parameter name when using AddWithValue, but I just noticed some code written by someone else that doesn't use it. Is one way more correct than the other?
cmd.Parameters.AddWithValue("ixCustomer", ixCustomer);
or
cmd.Parameters.AddWithValue("@ixCustomer", ixCustomer);
Use AddWithValue whenever you want to add a parameter by specifying its name and value. For SqlDbType Xml enumeration values, you can use a string, an XML value, an XmlReader derived type instance, or a SqlXml object.
All replies. Here, Add forces the conversion from string to date as it goes into the parameter. AddWithValue would have simply passed the string on to the SQL Server. Using Parameters.
C# SqlParameter is a handy feature allows you to safely pass a parameter to a SqlCommand object in . NET. A security best practice when writing . NET data access code, is to always use parameters in SqlCommand objects (whenever parameters are required of course).
No, both are equivalent in the end. I personally tend to use the notation with the @
sign myself, to be consistent with the T-SQL code for stored proc I write.
But as far as I know, both methods are fine for .NET apps interfacing with SQL Server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With