最近在阅读一些开源的代码,发现其中有些方法总结的很全面,至少在我做同样的事情时候,需要抓破脑袋想活着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类型的方法的更多相关文章

  1. 【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" ...

  2. 数据库字段值为null利用setInc方法无法直接写入

    1.数据库字段值为null利用setInc方法无法直接写入,先判断是否为空,再写入. if($points->add($dataList)){ $user=M('cuser'); $null=$ ...

  3. Oracle数据库字段类型说明

    目前Oracle 数据库大概有26个字段类型,大体分为六类,分别是字符串类型.数字数据类型.日期时间数据类型.大型对象(LOB)数据类型.RAW和LONG RAW数据类型.ROWID和UROWID数据 ...

  4. java.sql.Types,数据库字段类型,java数据类型的对应关系

    以下转自:http://kummy.itpub.net/post/17165/172850 本文在原文基础上有增减. 本概述是从<JDBCTM Database Access from Java ...

  5. Django数据模型——数据库字段类型

    字段 一个模型最重要也是唯一必需的部分,是它定义的数据库字段 字段名称限制 1.字段名不能是python保留字,这样会导致python语法错误 2.字段不能包含连续一个以上的下划线,这样会和Djang ...

  6. Oracle 数据库字段类型使用说明

    简介 目前Oracle 数据库大概有26个字段类型,大体分为六类,分别是字符串类型.数字数据类型.日期时间数据类型.大型对象(LOB)数据类型.RAW和LONG RAW数据类型.ROWID和UROWI ...

  7. ORACLE常用数据库字段类型

    ORACLE常用数据库字段类型   常用的数据库字段类型如下:   字段类型 中文说明 限制条件 其它说明  CHAR 固定长度字符串 最大长度2000 bytes     VARCHAR2 可变长度 ...

  8. SQL Server数据库字段类型说明

    SQL Server数据库字段类型说明 目前Sql Server 数据库一共有X个字段类型,大体分为9类,分别是字符串类型.二进制码字符串数据类型.Unincode字符串数据.整数类型.精确数据类型. ...

  9. SqlServer数据库表导入SqlLite数据库表保持日期时间类型字段的格式

    在写查询功能的过程中遇到一个这样的问题:按日期范围查询,sql语句是:where dt>=用户选择起始日期&&dt<=用户选择结束日期.数据库中的数据如图1,我选择的测试数 ...

随机推荐

  1. [swustoj 183] 种树

    种树(0183) 问题描述 Aconly有一块矩形的地,因为这块地里有很多石头,耕作很不方便,所以他打算在这块地上种一些果树.这块地用一个只含‘#’和‘*’的N*M的矩阵来表示,‘#’表示泥土,‘*’ ...

  2. mysql中php生成唯一ID

    <?php //uniqid官方手册 function create_guid($namespace = '') { static $guid = ''; $uid = uniqid(" ...

  3. hdu 2639 Bone Collector II

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. Mysql slave 状态之Seconds_Behind_Master

    在MySQL的主从环境中,我们可以通过在slave上执行show slave status来查看slave的一些状态信息,其中有一个比较重要的参数Seconds_Behind_Master.那么你是否 ...

  5. 进程间通信七 (socket)

    copyright:weishusheng          data:2015.5.26              摘要:socket又叫套接字或者插口,它也是进程间通信的一种方式,实际上就是网络上 ...

  6. MFC 状态栏相关使用(CStatusBar & CStatusBarCtrl)

    原文:MFC 状态栏相关使用(CStatusBar & CStatusBarCtrl),沙漠紫风铃 本文介绍了MFC中和状态栏相关的用法: 在MFC的的单文档应用中,在建好应用程序之后,CMa ...

  7. jdk1.6新特性

    1.Web服务元数据 Java 里的Web服务元数据跟微软的方案基本没有语义上的区别,自从JDK5添加了元数据功能(Annotation)之后,SUN几乎重构了整个J2EE体 系, 由于变化很大,干脆 ...

  8. Java:基础

    Hello World //HelloWorld.java文件 public class HelloWorld { public static void main(String[] args) { S ...

  9. PHP与MySQL动态网站开发2

    处理HTML表单 <input type="text" name="city"/> 通过$_REQUEST['city'];访问,$_REQUEST ...

  10. 项目用到了lua的哪些部分

    昨天有位同事跟我说,我们的手游客户端(cocos2d-x lua binding)代码没有看到lua的特殊技巧,用起来跟其他语言差不多.<Programming in lua>毕竟有将近4 ...