Tuesday, March 27, 2012

Enum datatype in SQL Server?

hi guys,

is it possible to do a enum datatype in SQL Server? if so can anyone point me to an example please?

thanks,

benny

No - there is no Enum Type is sql server.

Generally an Enum may be expressed in code as an Enum of Integers. Then in Sql Server, you would store the specific Integer that was selected. If you wish to insure that a value cannot be stored in your database unless it has been defined as allowable in sql server, then you could add a Constraint to your sql table for that particular Integer column to require specific values.

|||

thanks for your help :)

|||

I've found that it's effective enough to store the values of .NET Enums as int datatypes in SQL Server. Given that you have properties on your classes that are of type MyEnum, those int values will natively convert to their corresponding enum values (which are Integer values anyway).

Alternatively, if you store those values as varchar you can do an [Enum].Parse(GetType(MyEnum), VarcharValue) to convert it from its corresponding string.

Hope that helps.

No comments:

Post a Comment