【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 ...
随机推荐
- 使用JS的 FileReader 读取本地文本文件(可兼容各种浏览器)
最近需要在客户端操作文件,看到网上有 FileReader 对象,链接:https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader 还是直接 ...
- react native redux 草稿
Provider > Provider > 使组件层级中的 方法都能够获得 Redux store.正常情况下,你的根组件应该嵌套在 Provider 中才能使用 方法. 如果你真的不想把 ...
- mybatis include refid="Base_Column_List"含义
<sql id="Base_Column_List" > collegeID, collegeName </sql> <select id=" ...
- LeetCode 491. Increasing Subsequences
原题链接在这里:https://leetcode.com/problems/increasing-subsequences/ 题目: Given an integer array, your task ...
- strings命令(转)
以前我用strings命令的频率不高, 现在越用越顺手, 而且已经是离不开strings命令了.虽然以前说过strings命令, 但今天还是要说. 主要是两大用途, 下面来说明一下: 一. 确认代码编 ...
- [Java] key
Z2VueW1vJTIwJTI2JTI2JTI2JTIwMTYzJTNBJTBBdXNyJTIwLSUyMHd1a29uZ3N1bjEyMzQlMjAlMjMlMjAxNjMuc3VmZml4JTIw ...
- 如何进行seo优化要点总结
一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...
- ES6 fetch方法封装
// 请求路径 let url = 'http://jsonplaceholder.typicode.com/users' // 传输数据参数 const dataName = { name: &qu ...
- 第08组 Alpha冲刺(3/6)
队名:955 组长博客:https://www.cnblogs.com/cclong/p/11872693.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...
- tidyr
tidyr包主要提供了数据整理和清洗的功能,包括 1. 数据框的变形 2. 处理数据框中的空值 3. 根据一个表格衍生出其他表格 4. 实现行或列的分隔和合并 该包将要用的数据处理成标准且统一的数据框 ...