Sql Server 数据表实体化

DECLARE @TableName sysname = '表名';
DECLARE @Result VARCHAR(MAX) = 'public class ' + @TableName + '
{';

SELECT  @Result = @Result  + CHAR(13) +  '
    public ' + ColumnType + NullableSign + ' ' + colname + ' '  + ' { get; set; }'
FROM    (SELECT    column_id ColumnId,
                    ISNULL(t.desctr, '') AS desr,
                    col.name 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 'string'
                      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
                   LEFT  JOIN ( SELECT  ISNULL(CAST(g.value AS VARCHAR), '') desctr ,
                                        g.minor_id AS minor_id
                                FROM    sys.extended_properties g
                                WHERE   g.major_id = OBJECT_ID(@TableName)
                              ) t ON t.minor_id = col.column_id
                    JOIN sys.types typ ON col.system_type_id = typ.system_type_id
                                          AND col.user_type_id = typ.user_type_id   
          WHERE     object_id = OBJECT_ID(@TableName)
        ) t
ORDER BY ColumnId;

SET @Result = @Result + '
}';

PRINT @Result;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值