首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将SqlDbType转换为.net类型

将SqlDbType转换为.net类型
EN

Stack Overflow用户
提问于 2018-08-03 22:50:23
回答 1查看 878关注 0票数 2

我们如何从标准的.NET类或实例转换SqlDbType枚举的例子有很多。我还没有在互联网上找到任何反向解决方案。这似乎不太常见,但(我认为)应该是一种更简单的方法,然后在所有31个枚举成员上进行巨大的切换。

有谁知道如何以更好的方式将SqlDbType转换为.net类型,然后切换所有可能的枚举值?

感谢Tim Schmelter的评论。这似乎是唯一的解决方案。

EN

回答 1

Stack Overflow用户

发布于 2018-10-08 18:16:09

奇怪的是,.NET没有包含这个映射(而且SqlTypes也有点乱),但是“更好的方式”是一个依赖于枚举的字典。如果您的项目只支持可用列类型的子集,这也使得测试有效性变得很容易。我维护了一个处理动态定义的SQL表布局的项目。这样,我的库使用者只需要考虑SqlDbType,而不必担心内部的DataColumn映射。

代码语言:javascript
复制
internal static readonly Dictionary<SqlDbType, Type> equivalentSystemType = new Dictionary<SqlDbType, Type>
{
    { SqlDbType.BigInt, typeof(long) },
    { SqlDbType.Binary, typeof(byte[]) },
    { SqlDbType.Bit, typeof(bool) },
    { SqlDbType.Char, typeof(string) },
    { SqlDbType.Date, typeof(DateTime) },
    { SqlDbType.DateTime, typeof(DateTime) },
    { SqlDbType.DateTime2, typeof(DateTime) }, // SQL2008+
    { SqlDbType.DateTimeOffset, typeof(DateTimeOffset) }, // SQL2008+
    { SqlDbType.Decimal, typeof(decimal) },
    { SqlDbType.Float, typeof(double) },
    { SqlDbType.Image, typeof(byte[]) },
    { SqlDbType.Int, typeof(int) },
    { SqlDbType.Money, typeof(decimal) },
    { SqlDbType.NChar, typeof(string) },
    { SqlDbType.NVarChar, typeof(string) },
    { SqlDbType.Real, typeof(float) },
    { SqlDbType.SmallDateTime, typeof(DateTime) },
    { SqlDbType.SmallInt, typeof(short) },
    { SqlDbType.SmallMoney, typeof(decimal) },
    { SqlDbType.Time, typeof(TimeSpan) }, // SQL2008+
    { SqlDbType.TinyInt, typeof(byte) },
    { SqlDbType.UniqueIdentifier, typeof(Guid) },
    { SqlDbType.VarBinary, typeof(byte[]) },
    { SqlDbType.VarChar, typeof(string) },
    { SqlDbType.Xml, typeof(SqlXml) }
    // omitted special types: timestamp
    // omitted deprecated types: ntext, text
    // not supported by enum: numeric, FILESTREAM, rowversion, sql_variant
};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51675212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档