SqlCommand Parameters with SqlDbType and VarChar(MAX)

  • Thread starter Thread starter Zeke Jones
  • Start date Start date
Z

Zeke Jones

If I have a field in MS SQL 2005 called "data" with type VarChar(MAX),
how do I add a parameter using the syntax below if SqlDbType doesn't
have a VarChar(MAX) type. If I use SqlDbType.VarChar will the value get
truncated or is that what I should use?

SqlCommand cmd = new SqlCommand(query, connection);
cmd.Parameters.Add("@data", SqlDbType.VarChar).Value = largeTextData;

Or am I supposed to use the size parameter?

cmd.Parameters.Add("@data", SqlDbType.VarChar, 1000000).Value =
largeTextData;

Also how many characters does VarChar(MAX) hold?
 
You do not need to supp ly a size at the time you add the paramater (for any
type). It is infered by the provider when the command is executed.

As many as you can throw at it up to a maximum of 2^31-1 bytes (not that is
bytes and NOT characters).
 
Back
Top