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

想快速获取cs类结构
/// <summary>
/// T1
/// </summary>
public class T1
{
/// <summary>
/// 主键
/// </summary>
public int ID { get; set; } /// <summary>
/// 姓名
/// </summary>
public string NameLijhs { get; set; } }
通过运行下面的sql即可,先配置表名。
declare @TableName sysname = 'T1'
declare @Result varchar(max) = '
/// <summary>
/// ' + @TableName + '
/// </summary>
public class ' + @TableName + '
{' select @Result = @Result + '
/// <summary>
/// ' + CONVERT(NVARCHAR(500), ISNULL(ColName, '无')) + '
/// </summary>
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 = 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
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
效果如下:

完美,copy到项目里即可。
参考自:
利用SQL生成模型实体类
【SQL骚操作】SqlServer数据库表生成C# Model实体类SQL语句的更多相关文章
- SqlServer数据库表生成C# Model实体类SQL语句——补充
在sql语句最前边加上 use[数据库名] 原链接:https://www.cnblogs.com/jhli/p/11552105.html --[SQL骚操作]SqlServer数据库表生成C ...
- 【转发】SqlServer数据库表生成C# Model实体类SQL语句
已知现有表T1 通过运行下面的sql即可,先配置表名. declare @TableName sysname = 'T1' declare @Result varchar(max) = ' /// & ...
- 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 ...
- Java连接并操作SQLServer数据库
本人只需在项目中引入sqljdbc4.jar 包即可 ----------------------------------------- 在JAVA中如何连接SQL Server数据库 - hangh ...
- 5.7 Liquibase:与具体数据库独立的追踪、管理和应用数据库Scheme变化的工具。-mybatis-generator将数据库表反向生成对应的实体类及基于mybatis的mapper接口和xml映射文件(类似代码生成器)
一. liquibase 使用说明 功能概述:通过xml文件规范化维护数据库表结构及初始化数据. 1.配置不同环境下的数据库信息 (1)创建不同环境的数据库. (2)在resource/liquiba ...
- Electron中使用sql.js操作SQLite数据库
推荐sql.js——一款纯js的sqlite工具. 一.关于sql.js sql.js(https://github.com/kripken/sql.js)通过使用Emscripten编译SQLite ...
随机推荐
- 遗传算法介绍并附上Python代码
之前介绍过遗传算法,参见:https://www.cnblogs.com/LoganChen/p/7509702.html 我们用Python实现同样的问题解答. y=10*sin(5*x)+7*ab ...
- 用Python打造了一个渗透测试暴力探测器
资源探测的作用 通过资源探测,我们可以在目标系统中发现文件.目录.活动.服务还有相关的参数,为下一步的行动提供信息参考. 一个开源的模糊测试数据库 https://github.com/fuzzdb- ...
- Python下编写Windows自动化测试软件
https://www.jianshu.com/p/be3c46c7a905 uiautomation模块学习笔记 前段时间,由于个人需要,在网上查找了一些关于Windows平台下自动化测试的资料,最 ...
- 牛股资讯-PT
智能盯盘:实时监控股票涨跌极速行情:实时推送全球行情海量资讯:实时发布海量信息 股票平台,炒股软件,东方财富网,同花顺,大智慧,益盟操盘手,a股,沪深股市,创业板,交易策略,选股,大盘,牛股,牛市,财 ...
- AlexNet梳理
创新点 成功的使用relu函数替代了sigmoid函数,解决了使用sigmoid的梯度消散问题 成功的在全连接层使用dropout 成功的使用重叠最大池化 提出了LRN 利用GPU进行运算 数据增强2 ...
- JavaScript基础10——正则
什么是正则? 正则表达式(regular expression)是一个描述字符规则的对象.可以用来检查一个字符串是否含有某个字符,将匹配的字符做替换或者从某个字符串中取出某个条件的子串等. ...
- [教程]Ubuntu16.04安装texstudio
[教程]Ubuntu16.04安装texstudio step 1 首先要下载texlive. 不会的戳这里 然后直接终端命令 sudo apt-get install texstudio step ...
- (尚028)Vue_案例_交互删除
删除一条;1.鼠标移入移除这一条时颜色有变化 2.删除当前的todo ================================================================= ...
- ent 基本使用九 代码生成
ent 提供了cli 工具,可以方便我们进行schema 以及代码生成,同时目前提供的cli已经够用了 安装 cli go get github.com/facebookincubator/ent/c ...
- 【数论】[素数筛,phi]P3601签到题
题目描述 给出l,r,要求求出\(\sum_{i = l}^r (i - phi[i]) mod 666623333\) \(1\leq l\leq r\leq 10^{12}\),\(r - l \ ...