Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should I include the @ when using SqlCommand.Parameters.AddWithValue?

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);
like image 846
adambox Avatar asked Feb 22 '10 19:02

adambox


People also ask

What is SqlCommand parameters AddWithValue?

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.

What is the difference between ADD and AddWithValue?

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.

What is SQL parameter in C#?

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).


1 Answers

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.

like image 124
marc_s Avatar answered Sep 29 '22 00:09

marc_s