当前位置: 首页 > article >正文

将数据库表导出为C#实体对象

数据库方式

use 数据库;declare @TableName sysname = '表名'
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
(SELECTreplace(col.name, ' ', '_') ColumnName,column_id ColumnId,prop.value ColName,case typ.namewhen '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.nameend ColumnType,casewhen 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 NullableSignfrom sys.columns coljoin sys.types typ oncol.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_idLEFT JOIN sys.extended_properties prop ON col.object_id = prop.major_id AND col.column_id = prop.minor_idwhere object_id = object_id(@TableName)
) t
--order by ColumnIdset @Result = @Result  + '
}'print @Result

sqlsugar

using SqlSugar;namespace ConsoleApp3
{internal class Program{static void Main(string[] args){var dbName = "数据库名字";SqlSugarClient db = new SqlSugarClient(new ConnectionConfig(){ConnectionString = $"Server=localhost;uid=sa;pwd=123456789;Database={dbName};MultipleActiveResultSets=true;pooling=true;min pool size=5;max pool size=32767;connect timeout=20;Encrypt=True;TrustServerCertificate=True;",DbType = DbType.SqlServer,IsAutoCloseConnection = true,InitKeyType = InitKeyType.Attribute});var path = AppDomain.CurrentDomain.BaseDirectory;db.DbFirst.Where("TableName", "Student").CreateClassFile(path, "Test.Models");Console.WriteLine("生成成功");}}
}

参考

https://www.cnblogs.com/123525-m/p/18704699

http://www.lryc.cn/news/2404428.html

相关文章:

  • 物联网技术发展与应用研究分析
  • 金融系统渗透测试
  • C++ 信息学奥赛总复习题
  • 9.进程间通信
  • 性能剖析:在 ABP 框架中集成 MiniProfiler 实现性能可视化诊断
  • React 基础入门笔记
  • C++.OpenGL (12/64)光照贴图(Lightmaps)
  • 压测软件-Jmeter
  • Linux 常用命令语法总结
  • 青少年编程与数学 01-011 系统软件简介 02 UNIX操作系统
  • NLP学习路线图(三十):微调策略
  • leetcode刷题日记——1.组合总和
  • 关于单片机的基础知识(一)
  • Python训练营打卡Day45
  • Xilinx FPGA 重构Multiboot ICAPE2和ICAPE3使用
  • Redis专题-基础篇
  • springMVC-11 中文乱码处理
  • 【iOS安全】iPhone X iOS 16.7.11 (20H360) WinRa1n 越狱教程
  • MongoDB检查慢查询db.system.profile.find 分析各参数的作用
  • 智能标志桩图像监测装置如何守护地下电缆安全
  • 【网站建设】网站 SEO 中 meta 信息修改全攻略 ✅
  • 计算机视觉处理----OpenCV(从摄像头采集视频、视频处理与视频录制)
  • elasticsearch基本操作笔记
  • LVGL手势识别事件无上报问题处理记录
  • 《从零掌握MIPI CSI-2: 协议精解与FPGA摄像头开发实战》-- 第一篇:MIPI CSI-2基础入门
  • 变幻莫测:CoreData 中 Transformable 类型面面俱到(一)
  • 开源技术驱动下的上市公司财务主数据管理实践
  • 婚恋小程序直播系统框架搭建
  • day46 python预训练模型补充
  • CCPC chongqing 2025 H