Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

773 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