Last post Dec 05, 2015 08:51 PM by Mikesdotnetting
Dec 04, 2015 06:38 PM|JAYHAWKER|LINK
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;
Dec 04, 2015 10:13 PM|Mikesdotnetting|LINK
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.
Dec 04, 2015 11:23 PM|JAYHAWKER|LINK
So the way i am doing it specifying the datatype is plenty sufficient?
Dec 05, 2015 08:51 PM|Mikesdotnetting|LINK
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.