当前位置: 首页 > 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

相关文章:

  • Docke启动Ktransformers部署Qwen3MOE模型实战与性能测试
  • AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
  • 快速上手shell脚本运行流程控制
  • 1-2 Linux-虚拟机(2025.6.7学习篇- win版本)
  • 投屏技术深度解析:从原理到成功率优化实战·优雅草卓伊凡
  • Java Map完全指南:从基础到高级应用
  • 【LRU】 (最近最少使用)
  • 绘制饼图详细过程
  • 60天python训练计划----day46 and day47
  • 利用qcustomplot绘制曲线图
  • [yolov11改进系列]基于yolov11使用CPA-Enhancer自适应增强器替换backbone提高低照度目标的python源码+训练源码
  • 【C/C++】algorithm清单以及适用场景
  • Ntfs!NtfsWriteLog函数分析之ntfs!NTFS_LOG_RECORD_HEADER结构的一个例子----NTFS源代码分析之七
  • leetcode刷题日记——1.组合总和
  • 【论文解读】DeepSeek-R1
  • 常用存储器介绍
  • 操作系统:分页存储管理方式(精简版、含例题)
  • 无需登录即可使用的Web应用网站
  • MySQL事务与锁中的MVCC 深度解析与面试题讲解
  • C++学习-入门到精通【15】异常处理深入剖析
  • wordpress会员插件大全/徐州seo网站推广
  • wordpress站标签也打不开/参考消息今天新闻
  • 营销型网站建设的价格/网络营销工程师是做什么的
  • 嘉兴模板建站代理/品牌广告
  • 上海网站建设招聘/最新网络营销方式有哪些
  • wordpress商城 中文站/发稿