在项目中,我们很常见的就是需要根据数据库表去创建MODEL,自己一个字段一个字段的写,个人表示很麻烦,并且,如果没有特殊的要求的话,这样把所有的表写下来,很浪费时间,在这里,分享一个用sql快速创建MODEL,快速提高工作效率的方法:
第一步:将下面代码复制到sql里面,
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
declare @bm varchar(50),@zdxh int,@zdm varchar(30),@iszzz varchar(2),@isbszz varchar(1), @bszzzl varchar(20),@zj varchar(2),@Type varchar(30),@zjs numeric,@cd numeric, @xsws int,@is_null varchar(2),@mrz varchar(100),@zdsm varchar(1000) declare @tableName varchar(50),@s1 varchar(8000),@s2 varchar(8000),@s2_1 varchar(8000),@s2_2 varchar(8000),@s3 varchar(8000),@s31 varchar(8000),@s4 varchar(8000),@s5 varchar(8000) declare @ts1 varchar(50),@ts2 varchar(50),@ts3 varchar(50),@ts4 varchar(50),@ts5 varchar(50),@ts6 varchar(50) declare @i int,@s7 varchar(8000),@s8 varchar(8000),@s9 varchar(8000),@s10 varchar(8000),@s6 varchar(8000) set @i=0 set @tableName='K_News' print ' /*---------------------------------------------------------------------------------------- // 创建日期:'+replace(Convert(varchar(10),getdate(),102),'.','-')+' // 创建作者:小枫 http://www.cnblogs.com/mapleclever/ // 功能描述:对'+@tableName+'表的字段定义属性 // 修改日期: // 修改作者: // 修改描述: ----------------------------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Text; namespace MODEL { public class '+replace(@tableName,'K_','')+' { #region MODEL ' set @s1='' set @s2='' set @s2_1='' set @s2_2='' set @s3='' set @s31='' set @s4='' set @s5='' set @s6='' set @s7='' set @s8='' set @s9='' set @ts1='' set @ts2='' set @ts3='' set @ts4='' set @ts5='' set @ts6='' declare GroupID_cursor cursor for SELECT zdm=a.name, --字段名 type=b.name, --类型 zjs=a.length, --占用字节数 cd=COLUMNPROPERTY(a.id,a.name,'PRECISION'), --长度 xsws=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),--小数位数 is_null=case when a.isnullable=1 then '√'else '' end, --允许空 zdsm=isnull(Convert(varchar(1000),g.[value]),''), --字段说明 mrz=isnull(e.text,'') --默认值 FROM syscolumns a left join systypes b on a.xtype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' left join syscomments e on a.cdefault=e.id left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id where d.name=@tableName order by a.id,a.colorder open GroupID_cursor fetch GroupID_cursor into @zdm,@type,@zjs,@cd,@xsws,@is_null,@zdsm,@mrz while @@fetch_status=0 begin set @i=@i+1 set @Type=lower(@Type) set @ts3='_'+lower(@zdm) if @Type='bigint' or @Type='numeric' begin set @ts2='BigInt' set @ts1='int' end if @Type='numeric' begin set @ts2='BigInt' set @ts1='int64' end if @Type='binary' begin set @ts2='Binary' end if @Type='bit' begin set @ts2='Bit' set @ts1='bool' end if @Type='char' begin set @ts2='Char' set @ts1='string' end if @Type='datetime' begin set @ts2='DateTime' set @ts1='DateTime' end if @Type='decimal' begin set @ts2='Decimal' set @ts1='decimal' end if @Type='float' begin set @ts2='Float' set @ts1='float' end if @Type='image' begin set @ts2='Image' set @ts1='string' end if @Type='int' begin set @ts2='Int' set @ts1='int' end if @Type='money' begin set @ts2='Money' set @ts1='string' end if @Type='nchar' begin set @ts2='NChar' set @ts1='string' end if @Type='ntext' begin set @ts2='NText' set @ts1='string' end if @Type='nvarchar' begin set @ts2='NVarChar' set @ts1='string' end if @Type='real' begin set @ts2='Real' set @ts1='bool' end if @Type='smalldatetime' begin set @ts2='SmallDateTime' set @ts1='DateTime' end if @Type='smallint' begin set @ts2='SmallInt' set @ts1='int' end if @Type='smallmoney' begin set @ts2='SmallMoney' set @ts1='string' end if @Type='text' begin set @ts2='Text' set @ts1='string' end if @Type='timestamp' begin set @ts2='Timestamp' set @ts1='string' end if @Type='tinyint' begin set @ts2='TinyInt' set @ts1='int' end if @Type='uniqueidentifier' begin set @ts2='UniqueIdentifier' set @ts1='string' end if @Type='varbinary' begin set @ts2='VarBinary' set @ts1='string' end if @Type='varchar' begin set @ts2='VarChar' set @ts1='string' end if @Type='sql_variant' begin set @ts2='Variant' set @ts1='string' end set @s1=@s1+'private '+@ts1+' '+@ts3+';'+char(13)+char(10) if @i<20 begin set @s2=@s2+char(13)+char(10)+char(13)+char(10)+'/// <summary> /// '+@zdsm+' /// </summary> public '+@ts1+' '+@zdm+' { set { '+@ts3+' = value; } get { return '+@ts3+'; } }' end else if @i<40 begin set @s2_1=@s2_1+'/// <summary> /// '+@zdsm+' /// </summary> public '+@ts1+' '+@zdm+' { set { '+@ts3+' = value; } get { return '+@ts3+'; } }' end else begin set @s2_2=@s2_2+'/// <summary> /// '+@zdsm+' /// </summary> public '+@ts1+' '+@zdm+' { set { '+@ts3+' = value; } get { return '+@ts3+'; } }' end if @s3='' set @s3=@ts1+' '+@zdm else set @s3=@s3+', '+@ts1+' '+@zdm set @s4=@s4+'this.'+@ts3+'='+@zdm+';'+char(13)+char(10) fetch GroupID_cursor into @zdm,@type,@zjs,@cd,@xsws,@is_null,@zdsm,@mrz end close GroupID_cursor deallocate GroupID_cursor print @s1 print @s2 print @s2_1 print @s2_2 print 'public '+replace(@tableName,'K_','')+'() { } #endregion MODEL } } '
第二:修改相应的表名(set @tableName='表名'),点击执行即可
结果:
将生成的代码放到项目中即可.