none
DbCommand Parameters collection missing AddWithValue method

    Question

  • Hi,
    in every ADO.NET provider inside the xxxCommand class there's the Parameters collection that have a lot of "Add" method one of them is AddWithValue, that accept parameter's name and parameter's value.
    Using the agnostic DBCommand, there's only the Add method and AddWithValue is missing.
    You must create a new DbParameter with factory.CreateParameter() and than set Parameter name and value.
    Why is this usefull method missing in this class ???
    Thanks in advance.
    Thursday, August 20, 2009 7:27 PM

All replies

  • Hi delfo,

     

    I use reflector to look at the mechanism of DbParameterCollection and SqlParameterCollection.

    In the SqlParameterCollection’s AddWithValue method, it calls the constructor actually:

            public SqlParameter AddWithValue(string parameterName, object value)

            {

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

            }

     

    So it just do the same thing as you do in DbPrameter.

     

    And SqlParameterCollection inherits from DbParameterCollection. other ParameterCollections inherit form  it too.

    In my option, making the parent class neat is more favorable. When AddWithValue can be done by constructor, it is not very necessary to add it in the parent class although it is convenient. However, when it comes to child class, the effect to other classes is lower.


    You can download reflector form this URL: http://www.red-gate.com/products/reflector/
     

    Best Regards

    Yichun Feng


    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    • Marked as answer by Yichun_Feng Friday, August 28, 2009 3:19 AM
    • Unmarked as answer by Yichun_Feng Wednesday, October 14, 2009 8:04 AM
    Thursday, August 27, 2009 3:58 AM
  • Hi delfo,

     

    I use reflector to look at the mechanism of DbParameterCollection and SqlParameterCollection.

    In the SqlParameterCollection’s AddWithValue method, it calls the constructor actually:

            public SqlParameter AddWithValue(string parameterName, object value)

            {

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

            }

     

    So it just do the same thing as you do in DbPrameter.

     

    And SqlParameterCollection inherits from DbParameterCollection. other ParameterCollections inherit form  it too.

    In my option, making the parent class neat is more favorable. When AddWithValue can be done by constructor, it is not very necessary to add it in the parent class although it is convenient. However, when it comes to child class, the effect to other classes is lower.

    Hi, thanks for the reply.
    Your explanations are correct, but don't answer to my question.
    Why the classes derived from DbParameterCollection class have this method and the "parent" DbParameterCollection class don't have it ??
    I think that could be a good idea add a method like this;

        public DbParameter AddWithValue(string parameterName, object value)

            {

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

            }

    Clear, simple and usefull !!!
    Thank again.
    Stefano

    Sunday, October 11, 2009 9:15 PM
  • Here's a handy extension method that will help, as there is no constructor on DbParameter that takes arguments.

    /// <summary>
            /// Adds a parameter to the command.
            /// </summary>
            /// <param name="command">
            /// The command.
            /// </param>
            /// <param name="parameterName">
            /// Name of the parameter.
            /// </param>
            /// <param name="parameterValue">
            /// The parameter value.
            /// </param>
            /// <remarks>
            /// </remarks>
            public static void AddParameterWithValue(this DbCommand command, string parameterName, object parameterValue)
            {
                var parameter = command.CreateParameter();
                parameter.ParameterName = parameterName;
                parameter.Value = parameterValue;
                command.Parameters.Add(parameter);
            }
    Tuesday, March 08, 2011 7:27 PM