生成数据库表类 [英] Generate class from database table

查看:118
本文介绍了生成数据库表类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能在一个SQL Server生成表一类?



我不谈论使用一些ORM。我只需要创建的实体(简单类)。是这样的:

 公共类Person 
{
公共字符串名称{;设置; }
公共字符串电话{获取;设置; }
}


解决方案

设置@tablename到你的表的名称。

 宣布@tablename类型为sysname ='表名'
申报@Result VARCHAR(MAX)='公共类'+ @tablename +'
{'

选择@Result = @Result +'
公共'+ ColumnType + NullableSign +''+的ColumnName +'{搞定;组; ','_')的ColumnName,
}
'从

选择
替代(col.name,
'COLUMN_ID ColumnID的,
情况下typ.name
'的bigint'时再长
当'二进制'然后'的byte []'
当'位',那么'布尔'
当'字符'然后'日期',然后'日期时间'
当'日期时间',那么'日期时间'
当'DATETIME2'然后'日期时间'
当'DATETIMEOFFSET',那么字符串'时,
' '的DateTimeOffset'
当'小数'然后'小数'
'浮动'的时候再浮动
'形象'的时候再字节[]'
时,'廉政',那么'诠释'
时,'钱'然后'小数'
当'的nchar,然后字符
当'NTEXT'然后'串'
当数字,然后十进制'
当'nvarchar的',那么'串'
时,'真正的',然后'双'
当'SMALLDATETIME'然后'日期时间'
当'SMALLINT',那么'短'
'SMALLMONEY'时,那么'小数'
'文本'的时候再串$ b $当'时间',那么'时间跨度'
当'戳',那么'日期时间'
b当TINYINT,然后字节何时唯一标识符,然后的Guid'

当'VARBINARY'然后'的byte []'
'VARCHAR时,那么字符串'
,否则'UNKNOWN_'+ typ.name
端ColumnType,
情况下
时col.is_nullable = 1和typ.name在('BIGINT','位','日','日期时间,DATETIME2','DATETIMEOFFSET','小数','浮动','诠释','钱','数字','真','SMALLDATETIME','SMALLINT','SMALLMONEY','时间' TINYINT','唯一标识符)
然后'?'
,否则''
端NullableSign $ b $从山坳SYS.COLUMNS
b加入sys.types TYP上
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
,其中OBJECT_ID = OBJECT_ID(@tablename)
)笔
为了通过ColumnID的

设置@Result = @Result +'
}'

打印@Result


How can i generate a class from a table at a SQL Server?

I'm not talking about using some ORM. I just need to create the entities (simple class). Something like:

public class Person 
{
    public string Name { get;set; }
    public string Phone { get;set; }
}

解决方案

Set @TableName to the name of your table.

declare @TableName sysname = 'TableName'
declare @Result varchar(max) = 'public class ' + @TableName + '
{'

select @Result = @Result + '
    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
    select 
        replace(col.name, ' ', '_') ColumnName,
        column_id ColumnId,
        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
    where object_id = object_id(@TableName)
) t
order by ColumnId

set @Result = @Result  + '
}'

print @Result

这篇关于生成数据库表类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆