[英]what's the easiest way to detect the given object is value or reference type in .net?
[英]Given a T-SQL type as a string, what is the easiest way to evaluate it to a .Net Type?
如果給定一個包含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-類型-潛在的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;
}
嘗試以下功能。 我相信你可以根據自己的需要調整它。 如果錯過了什么,你會得到例外:
public static Type SqlTypeToType(string type)
{
string[] tokens = type.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
string typeFamily = tokens[0].ToLowerInvariant();
string size = tokens.Length > 1 ? tokens[1] : string.Empty;
switch (typeFamily)
{
case "bigint":
return typeof(long);
case "binary":
return size == "1" ? typeof(byte) : typeof(byte[]);
case "bit":
return typeof(bool);
case "char":
return size == "1" ? typeof(char) : typeof(string);
case "datetime":
return typeof(DateTime);
case "datetime2":
return typeof(DateTime);
case "decimal":
return typeof(decimal);
case "float":
return typeof(double);
case "image":
return typeof(byte[]);
case "int":
return typeof(int);
case "money":
return typeof(decimal);
case "nchar":
return size == "1" ? typeof(char) : typeof(string);
case "ntext":
return typeof(string);
case "nvarchar":
return typeof(string);
case "real":
return typeof(float);
case "uniqueidentifier":
return typeof(Guid);
case "smalldatetime":
return typeof(DateTime);
case "smallint":
return typeof(short);
case "smallmoney":
return typeof(decimal);
case "sql_variant":
return typeof(object);
case "text":
return typeof(string);
case "time":
return typeof(TimeSpan);
case "tinyint":
return typeof(byte);
case "varbinary":
return typeof(byte[]);
case "varchar":
return typeof(string);
case "variant":
return typeof(string);
case "xml":
return typeof(string);
default:
throw new ArgumentException(string.Format("There is no .Net type specified for mapping T-SQL type '{0}'.", type));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.