主流数据库字段类型转.Net类型的方法
最近在阅读一些开源的代码,发现其中有些方法总结的很全面,至少在我做同样的事情时候,需要抓破脑袋想活着google,现在看到了这个关于主流数据库字段类型转.Net类型的方法,故收藏之,也顺便分享给那些能看到这篇文章的同学。具体代码如下
/// <summary>
/// Default IDataType implementation (see IDataType for details)
/// </summary>
public class DataType : IDataType
{
public virtual string SqlType { get; set; }
public virtual string ManagedType { get; set; }
public virtual bool Nullable { get; set; }
public virtual long? Length { get; set; }
public virtual int? Precision { get; set; }
public virtual int? Scale { get; set; }
public virtual bool? Unsigned { get; set; }
public string FullType { get; set; }
} protected virtual Type MapDbType(string columnName, IDataType dataType)
{
if (dataType == null)
throw new ArgumentNullException("dataType");
if (dataType.ManagedType != null)
return Type.GetType(dataType.ManagedType, true); string dataTypeL = dataType.SqlType.ToLowerInvariant(); if (columnName != null && columnName.ToLower().Contains("guid"))
{
bool correctTypeAndLen =
((dataTypeL == "char" || dataTypeL == "varchar") && dataType.Length == )
|| ((dataTypeL == "binary") && dataType.Length == ); if (correctTypeAndLen)
{
Console.WriteLine("experimental support for guid--");
return typeof(Guid);
}
} switch (dataTypeL)
{
// string
case "c":
case "char":
case "character":
case "character varying":
case "inet":
case "long":
case "longtext":
case "long varchar":
case "mediumtext":
case "nchar":
case "ntext":
case "nvarchar":
case "nvarchar2":
case "string":
case "text":
case "varchar":
case "varchar2":
case "clob": // oracle type
case "nclob": // oracle type
case "rowid": // oracle type
case "urowid": // oracle type
case "tinytext": // mysql type
return typeof(String); // bool
case "bit":
case "bool":
case "boolean":
return typeof(Boolean); // int8
case "tinyint":
if (dataType.Length == )
return typeof(Boolean);
// tinyint is supposed to be signed
// but we can have explicit sign
if (dataType.Unsigned ?? false)
return typeof(Byte);
// default case, unsigned
return typeof(SByte); // int16
case "short":
case "smallint":
if (dataType.Unsigned ?? false)
return typeof(UInt16);
return typeof(Int16); // int32
case "int":
case "integer":
case "mediumint":
if (dataType.Unsigned ?? false)
return typeof(UInt32);
return typeof(Int32); // int64
case "bigint":
return typeof(Int64); // single
case "float":
case "float4":
case "real":
case "binary_float": // oracle type
case "unsigned float": // mysql type
case "float unsigned": // mysql type
return typeof(Single); // double
case "double":
case "double precision":
case "binary_double": // oracle type
case "unsigned double":// mysql type
case "double unsigned":// mysql type
return typeof(Double); // decimal
case "decimal":
case "money":
case "numeric":
return typeof(Decimal);
case "number": // special oracle type
if (dataType.Precision.HasValue && (dataType.Scale ?? ) == )
{
if (dataType.Precision.Value == )
return typeof(Boolean);
if (dataType.Precision.Value <= )
return typeof(Int16);
if (dataType.Precision.Value <= )
return typeof(Int32);
if (dataType.Precision.Value <= )
return typeof(Int64);
}
return typeof(Decimal); // time interval
case "interval":
return typeof(TimeSpan); //enum
case "enum":
case "set":
return MapEnumDbType(dataType); // date
case "date":
case "datetime":
case "ingresdate":
case "timestamp":
case "timestamp without time zone":
case "timestamp with time zone":
case "time":
case "time without time zone": //reported by twain_bu...@msn.com,
case "time with time zone":
return typeof(DateTime); // byte[]
case "binary":
case "blob":
case "bytea":
case "byte varying":
case "image":
case "longblob":
case "long byte":
case "oid":
case "sytea":
case "mediumblob":
case "tinyblob":
case "raw": // oracle type
case "long raw": // oracle type
case "varbinary":
return typeof(Byte[]); // PostgreSQL, for example has an uuid type that can be mapped as a Guid
case "uuid":
return typeof(Guid); case "void":
return null; // if we fall to this case, we must handle the type
default:
throw new ArgumentException(
string.Format("Don't know how to convert the SQL type '{0}' into a managed type.", dataTypeL),
"dataType");
}
}
主流数据库字段类型转.Net类型的方法的更多相关文章
- 【hibernate postgresql】注解@TypeDef/@Enumerated/数据库字段gender为枚举类型,从前台接受到实体后进行保存报错:org.postgresql.util.PSQLException: ERROR: column "gender" is of type gender but expression is of type character varying
数据库字段gender为枚举类型,从前台接受到实体后进行保存报错:org.postgresql.util.PSQLException: ERROR: column "gender" ...
- 数据库字段值为null利用setInc方法无法直接写入
1.数据库字段值为null利用setInc方法无法直接写入,先判断是否为空,再写入. if($points->add($dataList)){ $user=M('cuser'); $null=$ ...
- Oracle数据库字段类型说明
目前Oracle 数据库大概有26个字段类型,大体分为六类,分别是字符串类型.数字数据类型.日期时间数据类型.大型对象(LOB)数据类型.RAW和LONG RAW数据类型.ROWID和UROWID数据 ...
- java.sql.Types,数据库字段类型,java数据类型的对应关系
以下转自:http://kummy.itpub.net/post/17165/172850 本文在原文基础上有增减. 本概述是从<JDBCTM Database Access from Java ...
- Django数据模型——数据库字段类型
字段 一个模型最重要也是唯一必需的部分,是它定义的数据库字段 字段名称限制 1.字段名不能是python保留字,这样会导致python语法错误 2.字段不能包含连续一个以上的下划线,这样会和Djang ...
- Oracle 数据库字段类型使用说明
简介 目前Oracle 数据库大概有26个字段类型,大体分为六类,分别是字符串类型.数字数据类型.日期时间数据类型.大型对象(LOB)数据类型.RAW和LONG RAW数据类型.ROWID和UROWI ...
- ORACLE常用数据库字段类型
ORACLE常用数据库字段类型 常用的数据库字段类型如下: 字段类型 中文说明 限制条件 其它说明 CHAR 固定长度字符串 最大长度2000 bytes VARCHAR2 可变长度 ...
- SQL Server数据库字段类型说明
SQL Server数据库字段类型说明 目前Sql Server 数据库一共有X个字段类型,大体分为9类,分别是字符串类型.二进制码字符串数据类型.Unincode字符串数据.整数类型.精确数据类型. ...
- SqlServer数据库表导入SqlLite数据库表保持日期时间类型字段的格式
在写查询功能的过程中遇到一个这样的问题:按日期范围查询,sql语句是:where dt>=用户选择起始日期&&dt<=用户选择结束日期.数据库中的数据如图1,我选择的测试数 ...
随机推荐
- 转载:C++ vector 类学习笔记
声明:本文转载自http://blog.csdn.net/whz_zb/article/details/6827999 vector简介 vector是STL中最常见的容器,它是一种顺序容器,支持随机 ...
- poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...
- autofac 学习记录
builder.RegisterModule(new ConfigurationSettingsReader()); 需要注册上面一句才能读到.config里的节点,xml配置方式如下 <con ...
- UVa 247 (传递闭包) Calling Circles
题意: 有n个人m通电话,如果有两个人相互打电话(直接或间接)则在同一个电话圈里.输出所有电话圈的人的名单. 分析: 根据打电话的关系,可以建一个有向图,然后用Warshall算法求传递闭包. 最后输 ...
- BZOJ_1018_[SHOI2008]_交通堵塞traffic_(线段树)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1018 \(2*n\)的距形,起初没有边相连,之后有三种操作: 1.加边. 2.删边. 3.询问 ...
- BZOJ1758: [Wc2010]重建计划
题解: 这题我居然做了一星期?... 平均值的极值其实也可以算是一种分数规划,只不过分母上b[i]=1 然后我们就可以二分这个值.类似与 HNOI最小圈 如果没有 链的长度的限制的话,我们直接两遍df ...
- android源码GIT下载
mkdir device cd device git clone https://android.googlesource.com/device/common.git mkdir htc cd htc ...
- 流行的MySql版本
简介 MySQL是历史上最受欢迎的免费开源程序之一.它是成千上万个网站的数据库骨干,并且可以将它(和Linux)作为过去10年里Internet呈指数级增长的一个有力证明. 那么,如果MySQL真的这 ...
- c# equals与==的区别
对于值类型,如果对象的值相等,则相等运算符 (==) 返回 true,否则返回 false.对于string 以外的引用类型,如果两个对象引用同一个对象,则 == 返回 true.对于 string ...
- SpringContextHolder 静态持有SpringContext的引用(如何取得Spring管理的bean )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...