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


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)

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.