2016-03-09 191 views
3

在我的應用程序的服務器端,我使用MS SQL Server 2014 Express。在客戶端我有C#WPF應用程序,我有以下枚舉類型:如何在MS SQL Server 2014中創建新的數據類型?

/// <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 
} 

我可以創建這種類型的服務器端(在MS SQL Server數據庫),並使用這種類型的類型在數據庫表中的字段?

+0

** [CLR User-Defined Types](https://msdn.microsoft.com/en-us/library/ms131120.aspx)**。 SQL Server也有自己的CLR數據類型,如[hierarchyid](https://msdn.microsoft.com/en-us/library/bb677290.aspx)/ [geography](https://msdn.microsoft.com/en-us /library/cc280766.aspx)。 – lad2025

回答

0

我建議你去看看這個:Best method to store Enum in Database

你有什麼是枚舉,而不是一個類型。枚舉轉換爲類型Int。所以你會有這樣的事情:

/// <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 
} 
+0

有關於我的SQL,但我需要如何在MS SQL中執行此操作。 –

+0

正如我之前所說的,枚舉不是一個類型。您可以通過兩種方式實現相同的行爲:1:創建查找表 - http://blog.sqlauthority.com/2010/03/22/sql-server-enumerations-in-relational-database-best-practice/和2:創建一個檢查約束:http://www.databasejournal.com/features/mssql/article.php/3811831/Using-Check-Constraints-to-Validate-Data-in-SQL-Server.htm –

相關問題