在 switch 语句中使用标识字段
我有一个像这样的 SQL 查找表:
CREATE TABLE Product(Id INT IDENTITY PRIMARY KEY, Name VARCHAR(255))
我已将 ASP.NET DropDownList 数据绑定到 LLBLGen 实体。用户选择一个产品,并且 ID 被保存。现在我需要稍后显示一些产品特定的详细信息。我是否应该使用产品的 ID,并希望 ID 在安装之间始终相同?
switch (selectedProduct.Id)
{
case 1: //product one
break;
case 2:
case 3: //product two or three
break;
}
或者使用这个名字,并希望永远不会改变?
switch (selectedProduct.Name)
{
case "product one":
break;
}
或者有更好的选择吗?
I have a SQL lookup table like this:
CREATE TABLE Product(Id INT IDENTITY PRIMARY KEY, Name VARCHAR(255))
I've databound a ASP.NET DropDownList to a LLBLGen entity. User selects a product, and the Id get saved. Now I need to display some product specific details later on. Should I use the Product's ID, and hope the ID is always the same between installations ?
switch (selectedProduct.Id)
{
case 1: //product one
break;
case 2:
case 3: //product two or three
break;
}
or use the name, and hope that never changes?
switch (selectedProduct.Name)
{
case "product one":
break;
}
Or is there a better alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一些 ORM(至少 LLBLGen)可以为您处理这个问题;但生成强类型的枚举。不过我从来没有用过那个。
在这些情况下,我总是只使用我自己编写的枚举,但我确保所有字段都相同,并在发生任何更改时进行更新。当您跨数据库工作时(就像我一样),它会变得更有趣,但如果您小心的话,它就足够简单了。
Some ORMs (LLBLGen at least) can handle this for you; but generating a strong type of enums. I've never used that though.
In these cases, I always just go with an enum that I write myself, but I make sure that all the fields are equal, and update if any change. It becomes more interesting when you work across databases (as I do), but if you take care, it is simple enough.
如果您知道此表中的所有项目(我想如果您可以对它们进行切换,您就会知道)并希望每次安装它们都相同,那么也许它不应该是标识列,您应该插入 1, 2, 3、与产品本身有关。
If you know of all the items in this table (which I guess you do if you can do a switch on them) and want them the same for each installation then maybe it should not be an identity column and you should insert 1, 2, 3 with the products themselves.
对于这种情况,我见过三种常见的解决方案:
This lets you use an AlphaCode that is self-documenting, allows you to reinsert data without caring about the autoincrement PK value, and lets you change the product name without affecting anything. If you use the AlphaCode approach, ensure that you put a unique index on this column.
另一种解决方案(通常是最可取的)是将此逻辑移至数据库。例如,如果产品 1 是您在选择其类别时始终希望默认显示的产品,则可以向表中添加一个名为 IsHeroProduct 的列。那么你的查询就变成:
For this situation, there are three common solutions I have seen:
This lets you use an AlphaCode that is self-documenting, allows you to reinsert data without caring about the autoincrement PK value, and lets you change the product name without affecting anything. If you use the AlphaCode approach, ensure that you put a unique index on this column.
The other solution, which is often the most preferable one, is to move this logic to the database. E.g., if product 1 is the product you always want to show by default when its category is selected, you could add a column to your table called IsHeroProduct. Then your query becomes:
如果您希望固定 ProductID(这似乎不是一个好主意),那么您可以使用 IDENTITY INSERT(至少在 SQL Server 中)来确保 ProductID 值在装置。但是,我通常只会对静态参考数据执行此操作。
您还可以使用 Visual Studio 的 T4 模板直接从数据库数据生成
枚举
If you want your ProductID's to be fixed (which doesn't seem to be a good idea), then you can use
IDENTITY INSERT
(in SQL Server, at least) to ensure ProductID values are the same between installations. But, I would normally only do this for static reference data.You can also use Visual Studio's T4 templates to generate
enums
directly off the database data