locked
Am I correct that sqlcommand.Add has been replaced with AddWithValue? RRS feed

  • Question

  • User-858993849 posted

    I read that .Add is better than .AddWithValue, yet I also read that.Add has been depreciated.

    I use the following AddWithValue.  Is there anything wrong with it?

    string strColumnToSaveName = "test";
    object objValueToSave = 1;
    objCommand.Parameters.AddWithValue((strColumnToSaveName), SqlDbType.Int).Value = objValueToSave;

    Friday, December 4, 2015 6:38 PM

Answers

  • User-821857111 posted

    There are different .Add methods. The one that takes an object has been deprecated in favour of AddWithValue. The others take a number of parameters representing specific data types. They are often thought of as "better" because the database doesn't have to work out the data types of the parameter values. Having said that, the performance difference is probably marginal and not something that the vast majority of applications need to worry about.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 4, 2015 10:13 PM
  • User-821857111 posted

    You aren't specifying the data type. You are passing the data type in as the value of the parameter. Then you seem to be resetting the value using the .Value property. It's not something I've ever tried so I don't even know if that will work as you intend.

    Just for clarification, the second parameter of the AddWithValue method is an object that represents the actual value of the parameter. AddWithValue doesn't allow you to specify a data type.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 5, 2015 8:51 PM

All replies

  • User-821857111 posted

    There are different .Add methods. The one that takes an object has been deprecated in favour of AddWithValue. The others take a number of parameters representing specific data types. They are often thought of as "better" because the database doesn't have to work out the data types of the parameter values. Having said that, the performance difference is probably marginal and not something that the vast majority of applications need to worry about.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 4, 2015 10:13 PM
  • User-858993849 posted

    So the way i am doing it specifying the datatype is plenty sufficient?

    Friday, December 4, 2015 11:23 PM
  • User-821857111 posted

    You aren't specifying the data type. You are passing the data type in as the value of the parameter. Then you seem to be resetting the value using the .Value property. It's not something I've ever tried so I don't even know if that will work as you intend.

    Just for clarification, the second parameter of the AddWithValue method is an object that represents the actual value of the parameter. AddWithValue doesn't allow you to specify a data type.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 5, 2015 8:51 PM