首页 > 数据库 >sqlserver表生成C#实体类

sqlserver表生成C#实体类

时间:2023-02-25 11:11:16浏览次数:30  
标签:实体类 string C# sqlserver decimal when Result typ id

新建数据库查询,使用以下代码,记得修改表名称

 1 declare @TableName sysname = '表名称'
 2 declare @Result varchar(max) = 'public class ' + @TableName + '
 3 {'
 4 
 5 select @Result = @Result + '
 6     public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
 7 '
 8 from
 9 (
10     select 
11         replace(col.name, ' ', '_') ColumnName,
12         column_id ColumnId,
13         case typ.name 
14             when 'bigint' then 'long'
15             when 'binary' then 'byte[]'
16             when 'bit' then 'bool'
17             when 'char' then 'string'
18             when 'date' then 'DateTime'
19             when 'datetime' then 'DateTime'
20             when 'datetime2' then 'DateTime'
21             when 'datetimeoffset' then 'DateTimeOffset'
22             when 'decimal' then 'decimal'
23             when 'float' then 'double'
24             when 'image' then 'byte[]'
25             when 'int' then 'int'
26             when 'money' then 'decimal'
27             when 'nchar' then 'string'
28             when 'ntext' then 'string'
29             when 'numeric' then 'decimal'
30             when 'nvarchar' then 'string'
31             when 'real' then 'float'
32             when 'smalldatetime' then 'DateTime'
33             when 'smallint' then 'short'
34             when 'smallmoney' then 'decimal'
35             when 'text' then 'string'
36             when 'time' then 'TimeSpan'
37             when 'timestamp' then 'long'
38             when 'tinyint' then 'byte'
39             when 'uniqueidentifier' then 'Guid'
40             when 'varbinary' then 'byte[]'
41             when 'varchar' then 'string'
42             else 'UNKNOWN_' + typ.name
43         end ColumnType,
44         case 
45             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') 
46             then '?' 
47             else '' 
48         end NullableSign
49     from sys.columns col
50         join sys.types typ on
51             col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
52     where object_id = object_id(@TableName)
53 ) t
54 order by ColumnId
55 
56 set @Result = @Result  + '
57 }'
58 
59 print @Result

 

标签:实体类,string,C#,sqlserver,decimal,when,Result,typ,id
From: https://www.cnblogs.com/XL-Tommy/p/17153995.html

相关文章

  • ChatGPT 与 AIGC 简问乱答
    ChatGPT与AIGC简问乱答*仅代表个人观点。*[Q1]ChatGPT最近非常火爆,2个月突破1亿月活,从产品形态来看,我们知道的微软、谷歌的搜索引擎都会嵌入。那么我们如......
  • MacOS下载钉钉直播回放视频的Python最新解决方案
    tags:PythonMacOSTips写在前面之前写过一篇关于用Charles抓包下载钉钉直播回放视频的方法,那会还是可以直接通过​​FFmpeg​​​下载​​m3u8​​​链接并且直接合并的......
  • 基于springboot的restful接口的请求地址含有特殊字符被项目自带tomcat拦截问题
    写了一个基于springboot的restful接口,但按照设计,请求地址十分古怪,类似这样:http://192.168.10.8:8080/?skey=fb5e8ea9249c4ac19dad5e2a341e09ce&filter[]=A3011300500,eq,350......
  • 遥感数据机器学习的准备工作:python将栅格数据提取至EXCEL
    大部分我们处理的降水、气温等栅格数据的格式是nc形式,需要我们将他转换成栅格数据并导入至Arcgis中,进行下一步操作。之后我们根据自己的研究区进行裁剪【SpatialAnalyst......
  • ArcGIS处理nc数据
    降水温度蒸散发等气象数据通常以NC格式存储,可以用Matlab和ArcGIS读取数据。常为逐年逐月逐日数据。在用GIS进行数据分析时,需要将其转换为栅格数据。(1)打开nc数据。打开GIS,......
  • nginx、http、tomcat虚拟主机格式
    httpd<virtualhost *:80>servername ​​www.a.com​​documentroot /var/www/html</virtualhost>nginxserver{listen 80;server_name ​​www.a.com;​​root html;i......
  • [CCS12.1][CC2340] 环境搭建
    环境搭建​​一、CCS安装​​​​二、打补丁包​​​​三、环境修改​​一、CCS安装​​CCS12.0安装并设置中文​​​​CCS12.1.0.00007下载​​注:cc2340只能使用CCS1......
  • SpringBoot-使用链接字符串动态创建SqlSessionFactory执行任意SQL脚本
    SpringBoot-使用链接字符串动态创建SqlSessionFactory执行任意SQL脚本引言SpringBoot大大减少了使用XML配置的复杂性,但是想通过代码去实例化一个对象有点儿无从下手的感觉。......
  • Lightroom Classic for mac(Lrc2021) v10.4 激活版
    Lightroom Classic2021mac中文版是一款以后期制作为重点的图形工具软件,其增强的校正工具、强大的组织功能以及灵活的打印选项可以帮助您加快图片后期处理速度,将更多的时......
  • VScode设置过滤文件夹后,侧边栏搜索无过滤文件夹
    VScode设置过滤文件夹后,在侧边栏依旧可以搜索到仔细观看,可以发现在侧边栏排除文件这个搜索栏的右边,可以开启和关闭侧边栏~倘若你不知道在哪设置,可以ctrl+P,搜索setting......