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

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

相关文章:

  • EMC测试
  • 6月7日day47打卡
  • [ACTF2020 新生赛]Include 1(php://filter伪协议)
  • 嵌入:AI 的翻译器
  • golang常用库之-go-i18n库(国际化)
  • 26、跳表
  • SEO长尾词优化实战策略
  • 【大模型原理与技术-毛玉仁】第五章 模型编辑
  • leetcode刷题日记——二叉搜索树中第 K 小的元素
  • MIT 6.S081 Lab 11 networking
  • RD-Agent-Quant:一个以数据为中心的因素与模型联合优化的多智能体框架
  • CANoe trace里面显示的Time 具体是什么意思
  • Python抽象基类实战:构建广告轮播框架ADAM的核心逻辑
  • Python绘制三十六计
  • OGG 23ai for DAA 部署与补丁升级
  • 雪花ID问题诊断与解决方案
  • C++调试(肆):WinDBG分析Dump文件汇总
  • stm32内存踩踏一例
  • 高斯消元法及其扩展
  • 【2025年软考中级】第二章2.3 编译程序基本原理
  • 当数据包从上层移动到下层时,OSI 模型中会发生什么?
  • Go爬虫开发学习记录
  • 从内存角度透视现代C++关键特性
  • freeRTOS 互斥量优先级继承机制函数实现xQueueGenericReceive()
  • C++ 信息学奥赛总复习题答案解析(第一章)
  • 电脑商城--用户注册登录
  • 步进电机调试记录(先让我的步进电机转起来)
  • 【Java学习笔记】String类(重点)
  • 沉金电路板的黑盘缺陷挑战与解决方案——高密度互连设计的关键考量
  • Jina AI 开源 node-DeepResearch