主流数据库字段类型转.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,我选择的测试数 ...
随机推荐
- 在“BindingNavigator”删除数据前弹出确认框的实现
1)先设置DeleteItem为空,不让它调用自动生成的删除代码. 2)然后自己写代码实现,如下: private void bindingNavigatorDeleteItem_Click(obje ...
- CSS基础深入之细说盒子模型
Html任何一个元素(element)都可以当成一个盒子(box)来看待,可以结合现实中的盒子来理解下文,下文其中一些单词应该是通俗易懂的需要记录的单词. 基本情况 每一个盒子都有一个内容区域(con ...
- UVA 1658 Admiral 海上将军(最小费用流,拆点)
题意: 一个有v个点的有向图,要从点1到点v需要找两条路径,两路径不可经过同一个点(除了1和v点).求这两条路径的最小费用(保证有解). 分析: 难在建图,其他套模板. 此图给的是超级复杂图,两个点之 ...
- Action 操作
当鼠标移动到图片文件夹的时候,将有一些button显示 当鼠标移开这个文件夹,那些button隐藏了起来 display属性的变化 1.可以使用Js改变属性来操作 暂未验证,待时间. 2.可以使用Ac ...
- MAC下显示或者隐藏文件的命令
显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏Mac隐藏文件的命令:defaults writ ...
- codeforce 604B More Cowbell
题意:n个球,分成k堆.问堆的最大值的最小. #include <bits/stdc++.h> typedef long long ll; using namespace std; int ...
- Magento 处理方法
1.在模板中添加图片: <div id="header-image" class="skip-image"> <img src="& ...
- MFC定时器
比较简单,在程序中可以找到原型. 在程序中我们经常要使用定时刷新的功能,典型的应用是在信息管理系统中表单要跟着数据库中的数据变动.MFC提供了定时器来完成这个功能. ================= ...
- 【转载】C内存对齐
http://blog.csdn.net/hbuxiaofei/article/details/9491953 当你看到这个标题,仍想往下读的时候说明你已经开始关注数据在内存存储问题了. 好吧,下面先 ...
- 【Kafka入门】搭建Kafka本地环境
本博文介绍如何一步步搭建起Kafka本地环境. 下载Kafka 0.9.0.0 并配置软链接 下载好后,放入电脑本地安装目录,mac下我放在/usr/local下,解压Kafka. -0.9.0.0. ...