SqlServer数据库表生成C# Model实体类SQL语句

数据库表快速生成cs类的结构,类似:

/// <summary>
///  Student
/// </summary>
public class Student
{
    /// <summary>
    /// 主键    
    /// </summary>
    public int ID { get; set; }

    /// <summary>
    /// 姓名    
    /// </summary>
    public string Name { get; set; }

}

通过运行下面的sql即可:

declare @TableName sysname = 'T_ExamineeList'--表名
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
(
    SELECT
        replace(col.name, ' ', '_') ColumnName,
        column_id ColumnId,
        prop.value ColName,
        case typ.name
            when '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.name
        end ColumnType,
        case
            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')
            then '?'
            else ''
        end NullableSign
    from sys.columns col
        join sys.types typ on
            col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
            LEFT JOIN sys.extended_properties prop ON col.object_id = prop.major_id AND col.column_id = prop.minor_id
    where object_id = object_id(@TableName)
) t
--order by ColumnId

set @Result = @Result  + '
}'

print @Result

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SQL Server中,可以通过下列步骤将的结构生成C代码: 1. 打开SQL Server Management Studio(SSMS),连接到相应的数据库。 2. 在数据库中选中要生成C代码的。 3. 右键点击该,选择“脚本” -> “生成” -> “创建到” -> “文件”。 4. 在弹出的对话框中,选择要保存C代码的文件路径和文件名,点击“下一步”。 5. 在下一个对话框中,选择要生成的对象类型。对于生成的C代码,选择“”即可。可以选择性地勾选“生成带有外键的”选项。 6. 点击“完成”开始生成C代码。 7. 打开生成的C代码文件,即可查看的结构。 C代码的示例(仅含有的结构)如下: ```c #include <stdio.h> typedef struct { int id; char name[50]; double price; } Product; typedef struct { int id; int productId; int quantity; } Order; typedef struct { int id; char name[50]; char email[50]; } Customer; int main() { // Table: Product printf("Table: Product\n"); printf("id INT\n"); printf("name VARCHAR(50)\n"); printf("price FLOAT\n\n"); // Table: Order printf("Table: Order\n"); printf("id INT\n"); printf("productId INT\n"); printf("quantity INT\n\n"); // Table: Customer printf("Table: Customer\n"); printf("id INT\n"); printf("name VARCHAR(50)\n"); printf("email VARCHAR(50)\n"); return 0; } ``` 以上示例中,通过C结构体示每个的结构,通过printf语句输出结构信息。你可以根据实际需求进行调整和扩展。 这样,你就可以将SQL Server中的结构以C代码的形式生成和使用了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YuanlongWang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值