Solved

Difference between "Add" and "AddWithValue" for command parameters ...

Posted on 2007-11-29
4
8,550 Views
Last Modified: 2013-12-16
Hi,

I have the following info:

SqlCommand cmd = new SqlCommand("StoredProcedureName", cn);
cmd.Parameters.AddWithValue("@typ", dt);

What is the difference between "Add" and "AddWithValue" ? Means:

cmd.Parameters.Add(.....)
cmd.Parameters.AddWithValue(.....)

And when to use what ? Can you please explain me in simple terms with simple examples ?

Thanks
0
Comment
Question by:kishore_peddi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20378721
cmd.Parameters.AddWithValue("@typ", dt);
is equivalent to:
cmd.Parameters.Add("@typ", dt);

The later one is deprecated in favor of the first, as it is clearer.

Specify the data type of the parameter if you know it. Example:

cmd.Parameters.Add("@typ", SqlDbType.Int).Value = td;
0
 
LVL 16

Assisted Solution

by:SQL_SERVER_DBA
SQL_SERVER_DBA earned 240 total points
ID: 20379519
Deprecated is the keyword, here is the skinny if you like details....
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx
0
 

Author Comment

by:kishore_peddi
ID: 20380156
Can you please make this simple to understand with a small example ? I want to understand the basics behind the scenes.

AddWithValue replaces the SqlParameterCollection.Add method that takes a String and a Object. The overload of Add that takes a string and an object was deprecated because of possible ambiguity with the SqlParameterCollection.Add overload that takes a String and a SqlDbType enumeration value where passing an integer with the string could be interpreted as being either the parameter value or the corresponding SqlDbType value. Use AddWithValue whenever you want to add a parameter by specifying its name and value.

For SqlDbTypeXml enumeration values, you can use a string, an XML value, an XmlReader derived type instance, or a SqlXml object.

Appreciate your time, patience and help !!

Thanks
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 260 total points
ID: 20380474
The Parameters property gives you access to the ParameterCollection of the command object.

There are several ways of adding a parameter to the collection. You can create a parameter and add it:

SqlParameter answer = new SqlParameter("@answer", SqlDbType.Int);
answer.Value = 42;
cmd.Parameters.Add(answer);

You can use the Add method to create a parameter:

SqlParameter answer = cmd.Parameters.Add("@answer", SqlDbType.Int);
answer.Value = 42;

You can just use the return value of the Add method (a reference to the parameter) to set the value:

cmd.Parameters.Add("@answer", SqlDbType.Int).Value = 42;

You can use the AddWithValue method (but that doesn't let you specify the type):

cmd.Parameters.AddWithValue("@answer",42);
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question