SQL Server 根据表名创建实体类的字段和属性

 
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 
-- =============================================
-- Author:	 <shipeng.wang>
-- Create date: <2009-09-11>
-- Description:	<根据表名创建实体类的字段和属性>
-- =============================================
ALTER proc [dbo].[p_Wsp]
@tablename varchar(50)
as
declare @sql varchar(8000)
select @sql=isnull(@sql+char(9)+'private ','public class '+@tablename+char(13)+'{'+char(13)+char(9)+'private ')+
case when a.name in('image','uniqueidentifier','ntext','varchar','ntext','nchar','nvarchar','text','char') then 'string'
when a.name in('tinyint','smallint','int','bigint') then 'int'
when a.name in('datetime','smalldatetime') then 'DateTime'
when a.name in('float','decimal','numeric','money','real','smallmoney') then 'decimal'
when a.name ='bit' then 'bool'
else a.name end+' '+lower('_'+b.name)+';'+char(13)+char(9)+'public '+
case when a.name in('image','uniqueidentifier','ntext','varchar','ntext','nchar','nvarchar','text','char') then 'string'
when a.name in('tinyint','smallint','int') then 'int'
when a.name='bigint' then 'long'
when a.name in('datetime','smalldatetime') then 'DateTime'
when a.name in('float','decimal','numeric','money','real','smallmoney') then 'decimal'
when a.name ='bit' then 'bool'
else a.name end
+' '+b.name+char(13)+char(9)+'{'+char(13)+char(9)+char(9)+'get{return '+lower('_'+b.name)+';}'+
char(13)+char(9)+char(9)+'set{'+lower('_'+b.name)+'=value;}'+char(13)+char(9)+'}'+char(13)
from syscolumns b,
(select distinct name,xtype from systypes where status=0) a
where a.xtype=b.xtype and b.id=object_id(@tablename)
set @sql=@sql+'}'
print @sql
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值