【转发】SqlServer数据库表生成C# Model实体类SQL语句
已知现有表T1

通过运行下面的sql即可,先配置表名。
declare @TableName sysname = 'T1'
declare @Result varchar(max) = '
/// <summary>
/// ' + @TableName + '
/// </summary>
public class ' + @TableName + '
{' select @Result = @Result + '
public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
SELECT
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId,
prop.value ColName,
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 'char'
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 = 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
LEFT JOIN sys.extended_properties prop ON col.object_id = prop.major_id AND col.column_id = prop.minor_id
where object_id = object_id(@TableName)
) t
--order by ColumnId set @Result = @Result + '
}' print @Result

原文链接:https://www.cnblogs.com/jhli/p/11552105.html
【转发】SqlServer数据库表生成C# Model实体类SQL语句的更多相关文章
- SqlServer数据库表生成C# Model实体类SQL语句——补充
在sql语句最前边加上 use[数据库名] 原链接:https://www.cnblogs.com/jhli/p/11552105.html --[SQL骚操作]SqlServer数据库表生成C ...
- 【SQL骚操作】SqlServer数据库表生成C# Model实体类SQL语句
已知现有表T1 想快速获取cs类结构 /// <summary> /// T1 /// </summary> public class T1 { /// <summary ...
- SQL-49 针对库中的所有表生成select count(*)对应的SQL语句
题目描述 针对库中的所有表生成select count(*)对应的SQL语句CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_dat ...
- SQLServer数据库表架构和数据保存成sql文件
一.先在你的mssql数据库中点击“数据库–>任务–>生成脚本” 二.然后我们会看到有“生成和发布脚本”窗口 下一步 三.选择要编写脚本的数据库对象,全部导出选第一个,如果你想导出部分数据 ...
- 数据库已经存在表, django使用inspectdb反向生成model实体类
1.通过inspectdb处理类,可以将现有数据库里的一个或者多个.全部数据库表生成Django model实体类 python manage.py inspectdb --database defa ...
- python 从数据库表生成model
python 从数据库表生成model 找了很久才找到这个,我是新手... 现在已有建好的数据库,需要基于原有数据做数据分析的web应用,我选择python+Tornado ,由于不想写SQL语句,就 ...
- 由数据库表生成jpa实体工具
package cn.net.yto.aaa.dao.generator; /** * 由数据库表生成jpa实体工具 * * @author huike * Created by gf.liu on ...
- 5.7 Liquibase:与具体数据库独立的追踪、管理和应用数据库Scheme变化的工具。-mybatis-generator将数据库表反向生成对应的实体类及基于mybatis的mapper接口和xml映射文件(类似代码生成器)
一. liquibase 使用说明 功能概述:通过xml文件规范化维护数据库表结构及初始化数据. 1.配置不同环境下的数据库信息 (1)创建不同环境的数据库. (2)在resource/liquiba ...
- symfony 数据库表生成实体、迁移数据库
从数据库表生成实体 1. 由数据库生成模型: php bin/console doctrine:mapping:convert --from-database yml D:\db\ D:\test_b ...
随机推荐
- springboot如何读取配置文件中的参数(例如:application-consts.properties) 又结合maven读取配置文件的顺序
1.启动项目后,会读取pom.xml中的配置文件,例如现在读取的是本地配置 2.找到对应的配置文件 会读取uri地址下的配置.注:如果为springboot启动无需加config项目的名称,应该本身 ...
- 【优化】COUNT(1)、COUNT(*)、COUNT(常量)、COUNT(主键)、COUNT(ROWID)、COUNT(非空列)、COUNT(允许为空列)、COUNT(DISTINCT 列名)
[优化]COUNT(1).COUNT(*).COUNT(常量).COUNT(主键).COUNT(ROWID).COUNT(非空列).COUNT(允许为空列).COUNT(DISTINCT 列名) 1. ...
- Shell 编程 基础
本篇主要写一些shell脚本的基础知识,编程规范. 第一个shell脚本 [root@localhost ~]# vim first.sh #!/bin/bash # This is first Sh ...
- 在ubuntu系统中,遇到 “由于/bin 不在PATH 环境变量中,故无法找到该命令”问题
2018年01月07日 11:27:34 YangJianShuai 阅读数 3024更多 分类专栏: linux 好多命令的位置在/usr/bin.恢复办法如下:1. /usr/bin/sudo v ...
- Caused by: java.lang.IllegalStateException: duplicate key: datasource
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/applicatio ...
- centos安全加固
设置SSH登录超时时间 /etc/profile export TMOUT=900 设置账户密码策略 /etc/login.defs PASS_MAX_DAYS 180 PASS_MIN_DAYS 0 ...
- 异步模型:上下文与时间---async = request + reponse + handler + context + time;
futureHandler = current(handler, context(t0)) : T0 -> handler(context(t0),taskResult) : Tx ...
- 设置hystrix的熔断时间
hystrix的熔断时间默认为1秒,这对于一个要部署的服务器来说太短了,所以可以把这个时间设置大一点 这个时间设置在yml中没有提示,下面是设置的代码: hystrix: command: defau ...
- template-web.js 自定义过滤器
// 比如需要自定义一个去零的过滤器 <script id="templateTest" type="text/html"> <% for(i ...
- Mysql命令下导出select查询数据之 select ... into outfile方法
Mysql日常使用中经常遇到将select查询的数据导出到本地目录的情况,以便数据备份.分析等. 接下来将介绍Mysql终端下使用 select ... into outfile 语句导出数据方法 命 ...