枚举在数据库中 [英] Enums in a database

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

问题描述

在C#中,我可以设置一个枚举,以便将数字表示为关键字,这非常有用。


数据库中是否有这样的数据类型?


我想我可以使用一个额外的表,ID列作为数字,相应的列

,字符串代表关键字。这听起来有点低效 - 有更好的方法吗?

In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?

推荐答案



Jay写道:

Jay wrote:

在C#中,我可以设置一个Enum,这个数字表示为关键字,这非常有用。


是否有这样的数据类型数据库?


我想我可以使用一个额外的表,ID列作为数字,相应的列

,字符串代表关键字。这听起来有点低效 - 有更好的方法吗?
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?



您可以覆盖Enum的默认数值,如下所示:

(来自
的示例 http://www.c-sharpcorner.com/Languag...nCsharpRVS.asp


enum几个月

{

jan = 10,feb = 20,mar = 30,apr = 40

}

根据文章,不止一个枚举器可能具有相同的

值:


enum月份

{

jan = 1,feb = 1,mar,apr

}


要获得与枚举相关联的整数值,请将其强制转换为:


int x =(int)Months.jan;是一个有效的声明。

希望这会有所帮助...


Joel

You can override the default numeric values of an Enum like this:
(examples from
http://www.c-sharpcorner.com/Languag...nCsharpRVS.asp)

enum Months
{
jan = 10, feb = 20, mar = 30, apr=40
}
According to the article, more than one Enumerator may have the same
value:

enum Months
{
jan = 1, feb = 1, mar, apr
}

To get the integer value associated with the enum, cast it like so:

int x = (int) Months.jan ; is a valid statement.
Hope this helps...

Joel


周杰伦,

除非比我能想出的东西更聪明,我从来没有看到过这样的东西,恐怕你不得不使用查找表作为代号为

你的枚举。

彼得


-

联合创始人,Eggheadcafe .com开发者门户网站:
http://www.eggheadcafe.com

UnBlog:
http://petesbloggerama.blogspot.com


" Jay"写道:
Jay,
Unless sombody a lot smarter than I can come up with something I''ve never
seen, I am afraid you will have to use the lookup table as a surrogate for
your enums.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Jay" wrote:

在C#中我可以设置一个Enum,这个数字表示为关键字,这非常有用。


数据库中是否有这样的数据类型?


我想我可以使用一个额外的表,ID列作为数字,以及相应的列

表示关键字的字符串。这听起来有点效率低 - 有更好的方法吗?
In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?




使用查找表是标准方法。如果您在ac#class中也有

枚举值,那么很多时候您可以避免使用

查找表并直接查看数值数据。


我们发现有用的一件事是为每个枚举写一个单元测试

映射到查找表以验证枚举中的值

匹配查找表中的值。此外,如果查找表使用

一致的命名约定,您可以自动生成查询表数据的枚举类。


HTH,


Sam

-------------------------- ----------------------------------

我们正在招聘! B-Line Medical正在寻求Mid / Sr. .NET

开发人员在医疗产品中获得令人兴奋的职位

开发MD / DC。在轻松的团队环境中使用各种技术

。在Dice.com上查看广告。


星期五,2006年10月27日20:40:05 +0100,Jay < -wrote:

Using a lookup table is the standard approach. If you also have the
enum values in a c# class then a lot of times you can avoid using the
lookup table and look at the numeric data directly.

One thing we''ve found useful is to write a unit test for each enum
that maps to a lookup table to verify that the values in the enum
match the values in the lookup table. Also if the lookup tables use a
consistent naming convention you can generate the enum classes from
the lookup table data automatically.

HTH,

Sam
------------------------------------------------------------
We''re hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 27 Oct 2006 20:40:05 +0100, "Jay" <-wrote:

>在C#中,我可以设置一个Enum,以便将该数字表示为关键字,这非常有用。

我想我可以使用一个额外的表,ID列作为数字,相应的列
带有表示关键字的字符串。这听起来有点低效 - 有更好的方法吗?
>In C# I can set up an Enum so that number are represented as keywords, which is very useful.

Is there such a datatype in a database?

I suppose I could use an extra table, with the ID column as the number, and a corresponding column
with strings representing the keywords. This sounds a bit inefficient - is there a better way?


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

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