在数据库字段中枚举存储 [英] Enum storage in Database field

查看:203
本文介绍了在数据库字段中枚举存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好在数据库表格字段中存储枚举 或枚举 吗?



例如,我应该将'TJLeft'存储为字符串还是数据库中的等价值?

 公共枚举TextJustification 
TJLeft
TJCenter
TJRight
End Enum








有些枚举在我的控制之下,但有些是从第三方。

解决方案

是如果您在枚举中使用[Flags]属性,您可能需要允许多个枚举值。例如,你想让某人选择他们可用于某个星期几天...

  [ Flags] 
public enum WeekDays
{
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
星期五= 16
}

在这种情况下,您可以将数值存储在db对于值的任何组合(例如,3 ==星期一和星期二)


Is it best to store the enum value or the enum name in a database table field?

For example should I store 'TJLeft' as a string or a it's equivalent value in the database?

Public Enum TextJustification
  TJLeft
  TJCenter
  TJRight
End Enum

I'm currently leaning towards the name as some could come along later and explicitly assign a different value.

Edit -

Some of the enums are under my control but some are from third parties.

解决方案

Another reason to store the numeric value is if you're using the [Flags] attribute on your enumeration in cases where you may want to allow for multiple enumeration values. Say, for example you want to let someone pick what days of the week that they're available for something...

[Flags] 
public enum WeekDays     
{
   Monday=1,
   Tuesday=2,
   Wednesday=4,
   Thursday=8,
   Friday=16
}

In this case, you can store the numeric value in the db for any combination of the values (for example, 3 == Monday and Tuesday)

这篇关于在数据库字段中枚举存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆