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

将数据库表导出为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.lqws.cn/news/209341.html

相关文章:

  • 【Docker 01】Docker 简介
  • 论文略读:OmniKV: Dynamic Context Selection for Efficient Long-Context LLMs
  • QT进阶之路:带命名空间的自定义控件在Qt设计器与qss中的使用技巧
  • 多线程中的泛型应用深度解析:类型安全与并发编程的完美融合
  • 【Unity】模型
  • c++ 单例模式
  • 二.单例模式‌
  • nonlocal 与global关键字
  • vue3 创建图标 按钮
  • rl_sar实现sim2real的整体思路
  • Jetpack Compose瀑布流实现方案
  • 设计模式-观察着模式
  • 行为型设计模式之Interpreter(解释器)
  • C++常用的企业级日志库
  • DeepSeek-R1-0528:开源推理模型的革新与突破
  • 在Ubuntu22.04 系统中安装Docker详细教程
  • 低代码平台前端页面表格字段绑定与后端数据传输交互主要有哪些方式?华为云Astro在这方面有哪些方式?
  • 银行卡二三四要素实名接口如何用PHP实现调用?
  • Linux操作系统故障应急场景及对应排查方法
  • Linux 系统中的算法技巧与性能优化
  • code-server安装使用,并配置frp反射域名访问
  • 【PCIe总线】 -- PCI、PCIe相关实现
  • Java高级 | 【实验七】Springboot 过滤器和拦截器
  • WPF 播放器(AudioPlayer 2025)
  • vue · 插槽 | $slots:访问所有命名插槽内容 | 插槽的使用:子组件和父组件如何书写?
  • mysql已经安装,但是通过rpm -q 没有找mysql相关的已安装包
  • 【数据结构】6. 时间与空间复杂度
  • STM32学习笔记:外部中断(EXTI)原理与应用详解
  • 如何使用Jmeter进行压力测试?
  • NLP学习路线图(三十一): 迁移学习在NLP中的应用