主流数据库字段类型转.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,我选择的测试数 ...
随机推荐
- 1628. White Streaks(STL)
1628 题意不太好理解 求横黑条 和竖黑条共有多少个 注意1*1的情况 如果横向纵向都是1*1 算为一个 否则不算 用了下vector 枚举找下 #include <iostream> ...
- naotu.baidu.com 非常棒的脑图在线工具
1.png 2.txt 短租 前台功能 房源查看 房源搜索 城市房源 注册登录 预定房源 房源退订 在线支付 评价房源 个人中心 我的订单 我的账户 我的收藏 消息通知 管理员后台 房源发布 会员管理 ...
- sdut Message Flood(c++ map)
用字典树没过,学习了一下map; 参考博客:http://blog.csdn.net/zhengnanlee/article/details/8962432 AC代码 #include<iost ...
- Jquery动画第二部分
效果图: →→→→→→ <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...
- UVa 540 (团体队列) Team Queue
题意: 每个人都属于一个团体,在排队的时候,如果他所在的团体有人在队伍中,则他会站到这个团体的最后.否则站到整个队伍的队尾. 输出每次出队的人的编号. 分析: 容易看出,长队中,在同一个团体的人是排在 ...
- 【 D3.js 高级系列 — 5.0 】 颜色
颜色是作图不可少的概念,常用的标准有 RGB 和 HSL,D3 提供了创建颜色对象的方法,能够相互转换和插值. RGB色彩模式是通过对红(Red).绿(Green).蓝(Blue)三个颜色通道相互叠加 ...
- Maven配置文件Pom.xml详解
<project xmlns="http://maven.apache.org/POM/4.0.0 " xmlns:xsi="http://www.w3. ...
- NET下RabbitMQ实践[WCF发布篇]
在之前的两篇文章中,主要介绍了RabbitMQ环境配置,简单示例的编写.今天将会介绍如何使用WCF将RabbitMQ列队以服务的方式进行发布. 注:因为RabbitMQ的官方.net ...
- xmlns 属性
xmlns 属性 xmlns 属性可以在文档中定义一个或多个可供选择的命名空间.该属性可以放置在文档内任何元素的开始标签中.该属性的值类似于 URL,它定义了一个命名空间,浏览器会将此命名空间用于该属 ...
- codeforces 676D Theseus and labyrinth BFS搜索
分析:一个n*m的矩阵,每个格子有12个状态,每次按一次,每个格子转90度,所以整个矩阵只有4种状态,然后爆搜就好了 #include <cstdio> #include <iost ...