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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |