1. 程式人生 > 其它 >【SQL騷操作】SqlServer資料庫表生成C# Model實體類SQL語句

【SQL騷操作】SqlServer資料庫表生成C# Model實體類SQL語句

已知現有表T1

想快速獲取cs類結構

/// <summary>
///  T1    
/// </summary>
public class T1
{
    /// <summary>
    /// 主鍵    
    /// </summary>
    public int ID { get; set; }

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

}

通過執行下面的sql即可,先配置表名。

declare @TableName sysname = 'T1'
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

效果如下:

完美,copy到專案裡即可。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 declare@TableName sysname ='AUTHSYSTEMCHANGE'--表名 select*,'public '+ ColumnType + NullableSign +' '+ ColumnName +' { get; set; }'
asAutoCode from ( SELECT replace(col.name,' ','_') ColumnName, column_id ColumnId, prop.value ColName, casetyp.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 endColumnType, case whencol.is_nullable = 1andtyp.namein('bigint','bit','date','datetime','datetime2','datetimeoffset','decimal','float','int','money','numeric','real','smalldatetime','smallint','smallmoney','time','tinyint','uniqueidentifier') then'' else'' endNullableSign fromsys.columns col joinsys.types typon col.system_type_id = typ.system_type_idANDcol.user_type_id = typ.user_type_id LEFTJOINsys.extended_properties propONcol.object_id = prop.major_idANDcol.column_id = prop.minor_id whereobject_id = object_id(@TableName) ) t

  AutoCode列就是程式碼屬性

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 publicintID {get;set; } publicintAID {get;set; } publicstringProjectNM {get;set; } publicstringPMNo {get;set; } publicstringBRIEF {get;set; } publicstringProductType {get;set; } publicstringProductTypeOther {get;set; } publicstringSoftWareVersion {get;set; } publicstringReqCase {get;set; } publicstringReqCaseOther {get;set; } publicstringAuthLongTime {get;set; } publicstringApplicationState {get;set; } publicstringIPCNo {get;set; } publicstringBoardNo {get;set; } publicstringChangeCase {get;set; } publicstringCTIServerBackUpState {get;set; } publicstringProjectStage {get;set; } publicstringProjectType {get;set; } publicstringHSNFirst {get;set; } publicstringHSNSecond {get;set; } publicstringOprMethod {get;set; } publicDateTime OprTime {get;set; } publicstringApplicationMan {get;set; } publicDateTime ApplicationTime {get;set; } publicstringBasiceNote {get;set; } publicstringReceiver {get;set; } publicDateTime AuthLimetTime {get;set; } publicstringBusinessManager {get;set; } publicintSCENARIOTYPE {get;set; } publicintDOUBLESERVER {get;set; } publicintTIMELIMITType {get;set; } publicfloatTIMELIMITTime {get;set; } publicDateTime TIMELIMITDate {get;set; } publicintIsIVR {get;set; } publicintIVRCount {get;set; } publicintIsFIR {get;set; } publicintFIRCount {get;set; } publicintIsAGENT {get;set; } publicintAGENTCount {get;set; } publicintIsDEVICE {get;set; } publicintDEVICECount {get;set; } publicintIsNODEVAGT {get;set; } publicintNODEVAGTCount {get;set; } publicintIsPBX {get;set; } publicintIsNET {get;set; } publicintIsOEM {get;set; } publicintIsCCSERVICE {get;set; } publicintIsMANNUALDIAL {get;set; } publicintPROGRESSIVEDIAL {get;set; } publicintIsPREVIEWDIAL {get;set; } publicintIsIVRDIAL {get;set; } publicintIsSEPARATEDDIAL {get;set; } publicstringISNFirst {get;set; } publicstringRSNFirst {get;set; } publicstringISNSecond {get;set; } publicstringRSNSecond {get;set; } publicstringCheckPerson {get;set; } publicDateTime CheckTime {get;set; } publicDateTime WillOprTime {get;set; } publicintIsHSNFit {get;set; } publicintNumIsFit {get;set; } publicstringAuditorPerson {get;set; } publicDateTime AuditorTime {get;set; } publicstringOperationNote {get;set; } publicstringOperatonal {get;set; } publicstringChecking {get;set; } publicstringIsPassChange {get;set; } publicstringCheckingPerson {get;set; } publicDateTime CheckingTime {get;set; } publicstringPMIdea {get;set; } publicstringSCIdea {get;set; } publicstringCompanyName {get;set; } publicintIsAuth {get;set; } publicstringBusinessNote {get;set; } publicDateTime SuccessImplementDate {get;set; }

 轉載自:【SQL騷操作】SqlServer資料庫表生成C# Model實體類SQL語句 - 多安分 - 部落格園 (cnblogs.com)