开发者

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

开发者 https://www.devze.com 2023-01-26 15:26 出处:网络
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.

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 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. Use AddWithValue 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号