Difference between adding MySql Parameters with Add() and AddWithValue()
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 theAdd(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 theSqlParameterCollection.Add
method that takes a String and an Object. The overload ofAdd
that takes a string and an object was deprecated because of possible ambiguity with theSqlParameterCollection.Add
overload that takes a String and a SqlDbType enume开发者_JS百科ration value where passing an integer with the string could be interpreted as being either the parameter value or the corresponding SqlDbType value. UseAddWithValue
whenever you want to add a parameter by specifying its name and value.
Perhaps it is for the same reason.
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
精彩评论