3

On the server side of my application I use MS SQL Server 2014 Express. On client side I have C# WPF application and I have the following enum type there:

/// <summary>
/// User authorization level.
/// </summary>
public enum UserAuthorizationLevel
{
    /// <summary>
    /// Only visual inspection is allowed (the first lower level).
    /// </summary>
    Inspection,
    /// <summary>
    /// Some settings in apparatus are allowed (the second level).
    /// </summary>
    SettingDeviceParametersApplied,
    /// <summary>
    /// Some more setting in apparatus are allowed than in the second level (the third level).
    /// </summary>
    Maintenance,
    /// <summary>
    /// Apparatus manufacturer level - full access is allowed (the highest level).
    /// </summary>
    Manufacturer
}

Can I create this type on server side (in MS SQL Server database) and use this type as type for fields in database tables?

Dr.Doog
  • 197
  • 4
  • 14
  • **[CLR User-Defined Types](https://msdn.microsoft.com/en-us/library/ms131120.aspx)**. SQL Server also have own CLR datatypes like [hierarchyid](https://msdn.microsoft.com/en-us/library/bb677290.aspx)/[geography](https://msdn.microsoft.com/en-us/library/cc280766.aspx). – Lukasz Szozda Mar 09 '16 at 13:51

1 Answers1

0

I suggest you to take a look at this: Best method to store Enum in Database

What you have is an Enum, not a type. Enum translates to the type Int. So you will have something like this:

/// <summary>
/// User authorization level.
/// </summary>
public enum UserAuthorizationLevel
{
    /// <summary>
    /// Only visual inspection is allowed (the first lower level).
    /// </summary>
    Inspection = 0,
    /// <summary>
    /// Some settings in apparatus are allowed (the second level).
    /// </summary>
    SettingDeviceParametersApplied = 1,
    /// <summary>
    /// Some more setting in apparatus are allowed than in the second level (the third level).
    /// </summary>
    Maintenance = 2,
    /// <summary>
    /// Apparatus manufacturer level - full access is allowed (the highest level).
    /// </summary>
    Manufacturer = 3
}
Community
  • 1
  • 1
  • There is about my SQL there but I need how to do this in MS SQL. – Dr.Doog Mar 11 '16 at 10:20
  • As I said before, Enum is not a Type. You can achieve the same behavior by two ways: 1: Create an Lookup Table - http://blog.sqlauthority.com/2010/03/22/sql-server-enumerations-in-relational-database-best-practice/ and 2: Create a Check Constraint: http://www.databasejournal.com/features/mssql/article.php/3811831/Using-Check-Constraints-to-Validate-Data-in-SQL-Server.htm – Frederico Almeida Mar 11 '16 at 11:04