Solved

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

Posted on 2007-11-29
4
8,417 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
  • 2
4 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now