Solved

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

Posted on 2007-11-29
4
8,493 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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