Difference Between Parameters.Add(String, Object) and Parameters.Addwithvalue

Difference between Parameters.Add(string, object) and Parameters.AddWithValue

There is no difference in terms of functionality. In fact, both do this:

return this.Add(new SqlParameter(parameterName, value));

The reason they deprecated the old one in favor of AddWithValue is to add additional clarity, as well as because the second parameter is object, which makes it not immediately obvious to some people which overload of Add was being called, and they resulted in wildly different behavior.

Take a look at this example:

 SqlCommand command = new SqlCommand();
command.Parameters.Add("@name", 0);

At first glance, it looks like it is calling the Add(string name, object value) overload, but it isn't. It's calling the Add(string name, SqlDbType type) overload! This is because 0 is implicitly convertible to enum types. So these two lines:

 command.Parameters.Add("@name", 0);

and

 command.Parameters.Add("@name", 1);

Actually result in two different methods being called. 1 is not convertible to an enum implicitly, so it chooses the object overload. With 0, it chooses the enum overload.

Difference between SqlParameter.Add and AddWithValue

According to the MSDN:

AddWithValue replaces the SqlParameterCollection.Add method that takes
a String and an 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
.

So the AddWithValue replaces deprecated overload that create ambiguity.

SqlCommand Parameters Add vs. AddWithValue

Use Add if you want to make all explicit with a little bit more work. Use AddWithValue if you are lazy. AddWithValue will derive the type of the parameter of its value, so ensure that it's the correct type. You should, for example, parse a string to int if that is the correct type.

There is one reason to avoid Add: if your parameter type is int you must be careful with the overload that takes the parameter-name and an object since then another overload is chosen with the SqlDbType-enum.

From remarks (method overload is even obsolete now):

Use caution when you are using this overload of the
SqlParameterCollection.Add method to specify integer parameter values.
Because this overload takes a value of type Object, you must convert
the integral value to an Object type when the value is zero
... If you do not perform this conversion, the
compiler assumes that you are trying to call the
SqlParameterCollection.Add(string, SqlDbType) overload.

Parameters.AddWithValue vs Parameters.Add?

The first error is caused because you pass your connstr (string) instead of your conn (OracleConnection). Change this:

using (OracleCommand cmd =new OracleCommand(cmdstr,connstr))

To this

using (OracleCommand cmd =new OracleCommand(cmdstr,conn))

As for the second one, the OracleParameter does not seem to need :. Example use:

OracleCommand oraCommand = new OracleCommand("SELECT fullname FROM sup_sys.user_profile
WHERE domain_user_name = :userName", db);
oraCommand.Parameters.Add(new OracleParameter("userName", domainUser));

It seems like there is a difference between Oracle.DataAccess.Client and System.Data.OracleClient. OracleParameterCollection.AddWithValue seems to exist in System.Data.OracleClient. But you use Oracle.DataAccess.Client.

Parameters.Add(Overloads) vs Parameters.AddWithValue(Potentially losing characters)

The posted article already contains suggestions about how to avoid your problem.

Basically, you need to use the right overload of .Add:

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.add%28v=vs.110%29.aspx

As you can see from MSDN, there's only one overload marked [Obsolete]. I would use the most appropriate .Add overload that fits my needs.

Difference between adding MySql Parameters with Add() and AddWithValue()

When the documentation says nothing, consult the source.
These methods are identical (in their implementation):

/// <summary>
/// Adds a <see cref="MySqlParameter"/> to the <see cref="MySqlParameterCollection"/> given the specified parameter name and value.
/// </summary>
/// <param name="parameterName">The name of the parameter.</param>
/// <param name="value">The <see cref="MySqlParameter.Value"/> of the <see cref="MySqlParameter"/> to add to the collection.</param>
/// <returns>The newly added <see cref="MySqlParameter"/> object.</returns>
[Obsolete("Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value)")]
public MySqlParameter Add(string parameterName, object value)
{
return Add(new MySqlParameter(parameterName, value));
}

public MySqlParameter AddWithValue(string parameterName, object value)
{
return Add(new MySqlParameter(parameterName, value));
}

http://mysql-connector-net-5.0.sourcearchive.com/documentation/5.0.8.1/parameter__collection_8cs-source.html

what is difference betwn sqlcommand.parameters.add() and sqlcommand.parameters.addwithvalue()

The MSDN article on AddWithValue() explains the difference between the 2. Within is the explaination on the subtle differences.

AddWithValue replaces the SqlParameterCollection.Add method that takes a String and an 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.



Related Topics



Leave a reply



Submit