SQL生成 C# Model
本文转自: https://www.cnblogs.com/jhli/p/11552105.html
declare @TableName sysname = 'T_FakeOrderList'
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 '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
where object_id = object_id(@TableName)
) t
order by ColumnId set @Result = @Result + '
}' print @Result
SQL生成 C# Model的更多相关文章
- 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语句转为Model
在跟数据库打交道的时候,有一个常用的应用,就是把数据库中的表转为程序中的对象,也就是说表中的列转为对象的属性.对于字段比较少的,我们可以直接复制过去改,但是字段数比较多的时候,借助工具类实现比较方便而 ...
- c#保存datagridview中的数据时报错 “动态SQL生成失败。找不到关键信息”
ilovejinglei 原文 C#中保存datagridview中的数据时报错"动态SQL生成失败.找不到关键信息" 问题描述 相关代码 using System; us ...
- 重构 ORM 中的 Sql 生成
Rafy 领域实体框架设计 - 重构 ORM 中的 Sql 生成 前言 Rafy 领域实体框架作为一个使用领域驱动设计作为指导思想的开发框架,必然要处理领域实体到数据库表之间的映射,即包含了 OR ...
- 一条SQL生成数据字典
有个字典表并定期维护,对DBA和开发很重要,终于把他们整合在一起了,看有没问题? 一条SQL生成数据字典,包含所有OPEN用户.表名.字段名.字段序号.字段属性.默认值.是否非空.字段意思.主键标识. ...
- 对于不返回任何键列信息的 selectcommand 不支持 updatecommand 的动态 sql 生成
大家知道,DataSet保存的数据是位于服务器内存里面的原数据库的“副本”.所以用DataSet更新数据的过程就是先对“副本”进行更新,然后 在将“原本”更新,按照我的理解就是把“原本”覆盖掉.具体到 ...
- 查看Linq to Sql生成的sql语句(转)
查看Linq to Sql生成的sql语句 在控制台项目中,比较简单,直接db.Log = Console.Out;就OK了 但是在其他项目中,需要处理如下: StreamWriter sw = ...
- 怎样避免在EF自己主动生成的model中的DataAnnotation被覆盖掉
相信非常多人刚接触EF+MVC的时候,会有这个疑问.就是当我们在model类中加验证信息的时候.会在又一次生成model的时候被重写掉. 这里介绍一个方法: 比方我有个Employee类是从数据库中生 ...
随机推荐
- BZOJ 3207: 花神的嘲讽计划Ⅰ(莫队+哈希)
传送门 解题思路 刚开始写了个莫队+哈希+\(map\)的\(O(n\sqrt(n)log(n)\)的辣鸡做法,\(T\)飞了.后来看了看别人博客发现其实并不用拿\(map\)当桶存那些哈希值.因为只 ...
- ARM 是什么
ARM Advanced RISC Machines. RISC 就是reduced instruction set computer 精简指令集计算机DSP digtal signal Proces ...
- 【SQL】链接服务器
最近做项目,需要对两个数据库进行同步操作,所以采用在Server SQL中建立链接服务器方式实现. 链接服务器,可以直接访问/操作其他服务器上的数据库表. 1.连接SQL Server链接服务器 EX ...
- 如何将Vue项目部署到Nginx 服务器中
https://blog.csdn.net/qq_35366269/article/details/91385689
- Redis安装笔记
到官网查找到最新版本https://redis.io/download ————————————————设置防火墙入站规则 iptables -I INPUT -p tcp –dport -j ACC ...
- EmWin 如何显示汉字 不用在文件中使用编码
1. Font Converter for emWin 生成C文件字库 1.1 新建文件 1.2 选择字体 1.3 为了减小C文件体积,这里只加入自己需要的汉字,先把所有字体取消选择. 1.4 新建一 ...
- 7.Jmeter 快速入门教程--录制复杂web测试脚本
Jmeter的功能简单,不需要有脚本语言的编写经验,纯图形界面添加测试场景, 用起来上手很快.但是如果手动添加每一个web(http/https)请求,费时又费力.而且有可能最后手动编写的和实际发的请 ...
- [English]could, should, would, may 非單只是過去式
助動詞 could, should, would, may 的用法:(第三冊) 標題句:You should stay here. Would you like to order now? Could ...
- lzma 知识点滴
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/xuweiwei1860/article/details/31419195 LZMA(Lempel-Z ...
- sort的排序及使用
sort() 方法在适当的位置对数组的元素进行排序,并返回数组.数组会按照字符的Unicode进行排序(把数组里面当成字符串处理) 1. 按升序排列: var arr=[1,11,2,22,5,4,0 ...