RLH*_*RLH 5 .net c# t-sql types
如果给定一个包含SQL Server/T-SQL数据类型的字符串,那么将字符串计算为.Net类型的最简单方法是什么?
例如,如果您有一个包含" nvarchar " 的字符串,则转换方法返回的结果应该是System.StringType.如果我有一个包含" int " 的字符串,结果应该是一个System.Int32Type对象.
我可以轻松编写一个带有SQL数据类型字符串的函数,并通过返回.Net Type对象的switch/case语句发送字符串.但是,我不确定.Net框架中是否存在一个我忽略的功能.
将SQL Server数据类型解析为.Net数据类型的最简单/最正确的方法是什么?
附加背景
在我的例子中,我实际上有一个存储过程,它返回一些有关数据的元信息.具体来说,返回一个字符串字段,其中包含一个sql类型的值,该值可以是SQL Server 2005中可用的任何sql类型.
我的存储过程有返回任何SQL的类型-的潜力int,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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
926 次 |
| 最近记录: |