AddWithValue adds no value

AddWithValue adds no value

When sending parameters from C# to SQL Server, instead of .AddWithValue():

string SomeString = "some string";
...
cmdObject.Parameters.AddWithValue("@StringParam", SomeString);

Most suggest .Add() instead (e.g. "Steve" in this Stack Overflow answer):

string SomeString = "some string";
...
cmdObject.Parameters.Add(new SqlParameter
{
    ParameterName = "@StringParam",
    Value         = SomeString,
    SqlDbType     = SqlDbType.VarChar,
    Size          = 255 // given the column is varchar(255) (-1 for max)
});

Why? It allows you to be more explicit about the type being passed in, as well as the length, and has other performance benefits as well.

Aaron Bertrand

I am a passionate technologist with industry experience dating back to Classic ASP and SQL Server 6.5. I am a long-time Microsoft MVP, write at SQLPerformance and MSSQLTips, and have had the honor of speaking at more conferences than I can remember. In non-tech life, I am a father of two, a huge hockey and football fan, and my pronouns are he/him.