c# – 将T-SQL类型作为字符串,将它评估为.Net类型的最简单方法是什么?

前端之家收集整理的这篇文章主要介绍了c# – 将T-SQL类型作为字符串,将它评估为.Net类型的最简单方法是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果给定一个包含sql Server / T-sql数据类型的字符串,那么将字符串计算为.Net类型的最简单方法是什么?

例如,如果您有一个包含“nvarchar”的字符串,则转换方法返回的结果应该是System.String类型.如果我有一个包含“int”的字符串,结果应该是System.Int32 Type对象.

我可以轻松编写一个带有sql数据类型字符串的函数,并通过返回.Net Type对象的switch / case语句发送字符串.但是,我不确定.Net框架中是否存在一个我忽略的功能.

sql Server数据类型解析为.Net数据类型的最简单/最正确的方法是什么?

附加背景

在我的例子中,我实际上有一个存储过程,它返回一些有关数据的元信息.具体来说,返回一个字符串字段,其中包含一个sql类型的值,该值可以是sql Server 2005中可用的任何sql类型.

我的存储过程有可能返回任何sql-type,intint,smallint,datetime,binary等.我需要获取此数据类型并将其转换为.Net Type对象.

下面马修的评论确实提供了所有必要的映射信息,直接来自微软的文档,但同样,我想知道是否在System.Data或System.Data.sqlClient命名空间中集成了某些东西.

解决方法

我所知道的并没有任何暴露.在System.Data.sqlClient代码的深处,有一个用于确定类型映射的函数
internal Type GetTypeFromStorageType(bool issqlType)
{
    if (issqlType)
    {
        switch (this._type)
        {
            case StorageType.Empty:
                return null;

            case StorageType.Boolean:
                return typeof(sqlBoolean);

            case StorageType.Byte:
                return typeof(sqlByte);

            case StorageType.DateTime:
                return typeof(sqlDateTime);

            case StorageType.Decimal:
                return typeof(sqlDecimal);

            case StorageType.Double:
                return typeof(sqlDouble);

            case StorageType.Int16:
                return typeof(sqlInt16);

            case StorageType.Int32:
                return typeof(sqlInt32);

            case StorageType.Int64:
                return typeof(sqlInt64);

            case StorageType.Money:
                return typeof(sqlMoney);

            case StorageType.Single:
                return typeof(sqlSingle);

            case StorageType.String:
                return typeof(sqlString);

            case StorageType.sqlBinary:
                return typeof(object);

            case StorageType.sqlCachedBuffer:
                return typeof(sqlString);

            case StorageType.sqlGuid:
                return typeof(object);

            case StorageType.sqlXml:
                return typeof(sqlXml);
        }
    }
    else
    {
        switch (this._type)
        {
            case StorageType.Empty:
                return null;

            case StorageType.Boolean:
                return typeof(bool);

            case StorageType.Byte:
                return typeof(byte);

            case StorageType.DateTime:
                return typeof(DateTime);

            case StorageType.Decimal:
                return typeof(decimal);

            case StorageType.Double:
                return typeof(double);

            case StorageType.Int16:
                return typeof(short);

            case StorageType.Int32:
                return typeof(int);

            case StorageType.Int64:
                return typeof(long);

            case StorageType.Money:
                return typeof(decimal);

            case StorageType.Single:
                return typeof(float);

            case StorageType.String:
                return typeof(string);

            case StorageType.sqlBinary:
                return typeof(byte[]);

            case StorageType.sqlCachedBuffer:
                return typeof(string);

            case StorageType.sqlGuid:
                return typeof(Guid);

            case StorageType.sqlXml:
                return typeof(string);
        }
    }
    return null;
}

猜你在找的C#相关文章