0

One of our SQL Server column was recently modified from varchar to nvarchar, as a result the max_length value in sys.tables doubled. In the vb counterpart, our SQLMetadata objects were instantiated using the character length of the column, the last parameter of the constructor being the max length.

Should the SQLMetadata parameter be changed to use the nvarchar max length in bytes or left the way it is (character length of the SQL column)?

SQLMetadataObj = new SqlMetaData("Column1", SqlDbType.NVarChar, 12)

Any help would be appreciated

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Orion
  • 39
  • 3

1 Answers1

0

Presumably you mean sys.columns rather than sys.tables. What you're seeing is the number of bytes, not the number of characters. I just created two columns of type varchar(50) and nvarchar(50) and sys.columns shouwed a max_length of 50 and 100 respectively. The documentation for the SqlMetaData class that you posted that code example from specifically states:

The following example creates a new SqlMetaData object by specifying the column name, a column data type of SqlDbType.NVarChar, and a maximum length of 12 characters.

That means that you need to provide the number you see in the table designer, not in sys.columns.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Thanks, yes I should have said sys.columns rather than sys.tables. Sorry. Thanks for the clarification. – Orion Aug 11 '18 at 11:47
  • 1
    @Orion, the `CHARACTER_MAXIMUM_LENGTH` column of the INFORMATION_SCHEMA.COLUMNS` view contains the length in chararacters instead of bytes. – Dan Guzman Aug 11 '18 at 15:41