How to get SQL Server variable limit in C#

152 views Asked by At

I have a SQL Server table Workers and it has a column Name which is of datatype nvarchar(20).

I am using an ADO.NET Entity Data Model.

In my project, I want to limit user input to 20 characters (limit of my Name column).

How can I get this data through C# code and use it?

EDIT The solution I need is a C# code that gives me the 20(limit of my Name column) so I can dynamically use it as a validation data. By that, If I change this limit on my database it will automatically use the updated data so I won't go to my input and manually change it for new limit.

My table will be a c# class by EntityFramework so I will have a class named Workers and Name as a property of it. I am looking for the limit of this property.

2

There are 2 answers

0
marc_s On

All this information is stored in the SQL Server system catalog views in the sys schema.

You can e.g. get the max length of a particular column like this:

SELECT
    c.Name, c.max_length
FROM
    sys.columns c
INNER JOIN
    sys.tables t ON t.object_id = c.object_id
WHERE
    t.Name = 'Workers'
    AND c.Name = 'Name'
0
计科三班朱晓龙 On

Value is a reserved SQL word,so add [] to it.