在数据库字段中枚举存储

编程入门 行业动态 更新时间:2024-10-25 20:22:29
本文介绍了在数据库字段中枚举存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

例如,我应该将'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)

更多推荐

在数据库字段中枚举存储

本文发布于:2023-10-28 19:08:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537520.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!