数据集中的枚举
我正在使用 DataSet 将我的 C# 程序与 SQL 数据库连接起来。 我希望其中一列是枚举,并且希望它在我的代码中充当枚举。 我怎样才能做到这一点?
I'm using DataSet to connect my C# program with SQL database. I want one of the columns to be an enumeration and I want it to act as an enum in my code. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我并不是要戳破每个人的幻想,但您可以使用强类型数据集轻松地将整数映射到枚举。 我一直这样做。 我没有在这里输入整个内容,而是创建了一个 我的博客上的条目详细描述了如何实现这一点。
I don't mean to burst everyone's bubble, but you can easily map an integer to an Enum using a Strongly Typed DataSet. I do it all the time. Rather than type the whole thing out here I have created an entry on my Blog describing in detail how to accomplish this.
我认为你不能。 SQL Server 没有枚举的概念。
I don't think you can. SQL Server doesn't have a concept of enums.
我建议使用位于存储库层之上的 ApplicationService 层。 然后,在您的 ApplicationService 类(考虑该类的适当名称)中,您可以将从存储库层返回的数据转换为 POCO 对象中适当的枚举值。
I would suggest using an ApplicationService layer that sits atop your repository layer. Then in your ApplicationService class (think of appropriate name for this class) you can transform the data that is returned from the repository layer to the appropriate enum value in your POCO object.
您可以尝试使用强类型数据集。
You might try using a strongly typed dataset.
从技术上讲你不能。 枚举是静态类型的,它们被设计为当您在编译时知道所有值时使用。 虽然有一些解决方法,但我强烈建议您不要这样做。
查看不可修改的数据集,这将提供枚举的大部分好处,并且可以动态创建。
虽然数据不能存储为枚举,但只要数据库列中的数据是整数类型,它们就可以用作奇特的过滤器。 如果这就是您所追求的,那是一个完全不同的问题。 转到 MSDN 页面并阅读在枚举上。
Technically you can't. Enum's are static typed, they are designed to be used when you know all of the values at compile time. While there are some work-arounds, I would highly recommend that you do not do this.
Look at a unmodifiable dataset, this will give most of the benefits of a Enum and can be created on the fly.
While data cannot be stored as an Enum, they can be used as a fancy filter as long as the data in the database column is of an integer type. That is a complete different question if that is what you are after. Go to the MSDN page and read up on Enum's.