我们如何从标准的.NET类或实例转换SqlDbType枚举的例子有很多。我还没有在互联网上找到任何反向解决方案。这似乎不太常见,但(我认为)应该是一种更简单的方法,然后在所有31个枚举成员上进行巨大的切换。
有谁知道如何以更好的方式将SqlDbType转换为.net类型,然后切换所有可能的枚举值?
感谢Tim Schmelter的评论。这似乎是唯一的解决方案。
发布于 2018-10-08 18:16:09
奇怪的是,.NET没有包含这个映射(而且SqlTypes也有点乱),但是“更好的方式”是一个依赖于枚举的字典。如果您的项目只支持可用列类型的子集,这也使得测试有效性变得很容易。我维护了一个处理动态定义的SQL表布局的项目。这样,我的库使用者只需要考虑SqlDbType,而不必担心内部的DataColumn映射。
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
};https://stackoverflow.com/questions/51675212
复制相似问题