2

According to MySql in this document C.7.9.6. Changes in MySQL Connector/NET 5.0.5 (07 March 2007):

Added MySqlParameterCollection.AddWithValue and marked the Add(name, value) method as obsolete.

I've been using .Add up until recently and experienced no problems. Upon discovering the .AddWithValue method, it is preferable primarily because it involves less syntax.

My question: does anyone know if there is any functional difference between the two methods? I cannot find proper documentation on them.

Edit:

Microsoft makes this note about SqlParameterCollection:

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.

Perhaps it is for the same reason.

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't even think to check the source! Thanks for both the information and the reminder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.