使用Sql语句快速将数据表转换成实体类
开发过程中经常需要根据数据表编写对应的实体类,下面是使用sql语句快速将数据表转换成对应实体类的代码,使用时只需要将第一行'TableName'引号里面的字母换成具体的表名称就行了:
declare @TableName sysname = 'TableName'
declare @Result varchar(max) = 'public class ' + @TableName + '
{' select @Result = @Result + '
public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
select
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId,
case typ.name
when 'bigint' then 'long'
when 'binary' then 'byte[]'
when 'bit' then 'bool'
when 'char' then 'string'
when 'date' then 'DateTime'
when 'datetime' then 'DateTime'
when 'datetime2' then 'DateTime'
when 'datetimeoffset' then 'DateTimeOffset'
when 'decimal' then 'decimal'
when 'float' then 'float'
when 'image' then 'byte[]'
when 'int' then 'int'
when 'money' then 'decimal'
when 'nchar' then 'string'
when 'ntext' then 'string'
when 'numeric' then 'decimal'
when 'nvarchar' then 'string'
when 'real' then 'double'
when 'smalldatetime' then 'DateTime'
when 'smallint' then 'short'
when 'smallmoney' then 'decimal'
when 'text' then 'string'
when 'time' then 'TimeSpan'
when 'timestamp' then 'DateTime'
when 'tinyint' then 'byte'
when 'uniqueidentifier' then 'Guid'
when 'varbinary' then 'byte[]'
when 'varchar' then 'string'
else 'UNKNOWN_' + typ.name
end ColumnType,
case
when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
then '?'
else ''
end NullableSign
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
where object_id = object_id(@TableName)
) t
order by ColumnId set @Result = @Result + '
}' print @Result
使用Sql语句快速将数据表转换成实体类的更多相关文章
- C# 将DataTable数据源转换成实体类
using System; using System.Collections.Generic; using System.Data; using System.Reflection; /// < ...
- OLEDB不使用SQL语句直接打开数据表
一般来说获取数据库表的方法是采用类似 select * from table_name 这样的sql语句.SQL语句必然伴随着数据库的解释执行,一般来说效率比较低下,而且使用SQL语句时需要数据库支持 ...
- mysql中通过sql语句查询指定数据表的字段信息
mysql数据库在安装完成时,自动创建了information_schema.mysql.test这三个数据库.其中,information_schema记录了创建的所有数据库的相关信息,因此可以 ...
- 数据表转换成json(DatatableToJson)
#region 转换Table为JSON数据 /// <summary> /// 转换Table为JSON数据 /// </summary> /// <param nam ...
- hibernate sql查询后对象转换成实体类
在多表查询的时候使用hibernate的sql查询的时候,一般返回的是object[]数组,或者可以使用 session.createSQLQuery(sql).setResultTransform ...
- sql语句将身份证号数字转换成特殊字符
SELECT Tname , STUFF(Idcard,,,'*********') as Idcard,Completion from demo
- 一个用于将sql脚本转换成实体类的js代码
以前写过一段C#,苦于编译才能用.这样的小工具最好是用脚本语言来编写,易于执行,也易于修改. js 代码 convert.js ------------------------------------ ...
- 远程调用get和post请求 将返回结果转换成实体类
package org.springblade.desk.utils; import org.apache.http.client.ResponseHandler; import org.apache ...
- DataTable转换成实体
public static class DataTableToEntity { /// <summary> /// 将DataTable数据源转换成实体类 /// </summary ...
随机推荐
- TMS320VC5509驱动AT24C02
1. 刚开始的波形不太对,比如如下代码 i2c_status = I2C_write( at24c02_write_buf, //pointer to data array , //length of ...
- 3、Docker容器管理
一.容器创建 1.创建命令 docker container [root@localhost harbor]# docker container Usage: docker container CO ...
- CSS快速入门-属性和伪类
一.属性选择器 <div class="gradefather"> hello1 <div name="son">hello2 < ...
- 软件测试_测试工具_LoadRunner
最近正在逐步学习软件测试工具的使用,此文章也是用来当做笔记以供记录之用.如有问题,还请多多指出. 安装LoadRunner基本步骤从网上搜索即可找到,特此提供部分链接参考(其中附带软件下载): 1.L ...
- Zookeeper 通知更新可靠吗? 解读源码找答案!
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由特鲁门发表于云+社区专栏 导读: 遇到Keepper通知更新无法收到的问题,思考节点变更通知的可靠性,通过阅读源码解析了解到zk Wa ...
- Python学习-将Python3.3.4还原成Python2.6版本
http://blog.csdn.net/dream_angel_z/article/details/45724515
- Linux第五章笔记
5.1 与内核通信 系统调用在用户空间进程和硬件设备之间添加了一个中间层. 主要作用有: 为用户空间提供了一种硬件的抽象接口 系统调用保证了系统的稳定和安全 每个进程都需要运行在虚拟机内 5.2 AP ...
- linux 常用命令-变量命令
想要的结果,有时候我们想使用上一句命令的执行结果,当然可以通过鼠标去复制粘贴,但是这样既不库又效率低,所以想能不能通过快捷键获取上一句命令的值执行结果呢,答案是不能,后来想如果能把执行结果存入变量那不 ...
- ElasticSearch 2 (20) - 语言处理系列之如何开始
ElasticSearch 2 (20) - 语言处理系列之如何开始 摘要 Elasticsearch 配备了一组语言分析器,为世界上大多数常见的语言提供良好的现成基础支持. 阿拉伯语.亚美尼亚语,巴 ...
- php实现文件上传,下载的常见文件配置
配置文件,php.ini uploadfile post_max_size 规定表单上传的最大文件: