What size do you use for varchar(max) in a SqlCommand parameterized SqlCommand object?

Posted by Blake on 9/9/2013
)

Question:

What size do you use for a varchar(max) in a SqlCommand parameter?

Answer:

For varchar(max) you can use –1 for the length.

VB.Net

    ' Adding it with the value at the same time
    cmd.Parameters.Add("@file_text", SqlDbType.VarChar, -1).Value = "the text you want to set"

    ' Adding it with just the parameter
    cmd.Parameters.Add("@file_text", SqlDbType.VarChar, -1)

C#

    // Adding it with the value at the same time
    cmd.Parameters.Add("@long_text", SqlDbType.VarChar, -1).Value = "the text you want to set";

    // Adding it just with the parameter
    cmd.Parameters.Add("@long_text", SqlDbType.VarChar, -1)