ITPub博客

首页 > 数据库 > SQL Server > SQL Server 生成C#公共实体属性和私有属性

SQL Server 生成C#公共实体属性和私有属性

原创 SQL Server 作者:胡田新 时间:2020-12-07 14:47:25 0 删除 编辑


declare @TableName sysname = 'checkDate'--我的表名

SELECT 'private '+ case [Types].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_' + [Types].name

        end +' _'+ [Columns].name+';' FROM sys.tables AS [Tables]

INNER JOIN sys.columns AS [Columns] ON [Tables].object_id = [Columns].object_id

INNER JOIN sys.types AS [Types] ON [Columns].system_type_id = [Types].system_type_id

AND is_user_defined = 0 AND [Types].name <> 'sysname' 

LEFT OUTER JOIN sys.extended_properties AS [Properties] ON [Properties].major_id = [Tables].object_id

AND [Properties].minor_id = [Columns].column_id AND [Properties].name = 'MS_Description' 

WHERE [Tables].name =@TableName ORDER BY [Columns].column_id

-----------------------------------------------私有实体-------------------------------------------------



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{return _'+ColumnName+';} 

    set{_'+ColumnName+'=value;} }'

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


-----------查询结果----------

private int _sid;

private int _suid;


/// <summary>

///  checkDate    

/// </summary>

public class checkDate

{

    /// <summary>

    /// 主键    

    /// </summary>

    public int sid { 

    get{return _sid;} 

    set{_sid=value;} }

    /// <summary>

    /// 用户id    

    /// </summary>

    public int suid { 

    get{return _suid;} 

    set{_suid=value;} }

}





来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31486267/viewspace-2740018/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2017-08-30

  • 博文量
    19
  • 访问量
    26881