首页 > 代码库 > 机器Coding For WinForm
机器Coding For WinForm
declare @objname varchar(90)=‘AutoProjectModel‘
declare @modelns varchar(90)=‘ACL.Models‘
declare @dpclsname varchar(200)=‘ACL.UPDExtDataProvider‘--model数据提供类名
declare @datamodel varchar(90)=‘ext_‘ +@objname
declare @clsname varchar(90)=replace(@datamodel,‘ext_‘,‘‘)+‘Model‘
declare @modelname varchar(90)=‘自动化项目模块‘
--type 0:注释,1:访问方法,2:model,3:dataprovider,4:viewmodel,5:codebehind,6:ListViewUI,7:DatagridUI,8读存储过程,9写存储过程
create table #(myfd varchar(max))
declare @readprocname varchar(90)=‘‘,@writeprocname varchar(90)=‘‘
set @readprocname=‘wp_getPager_‘+@objname+‘_ds‘
set @writeprocname=‘wp_‘+@objname+‘_change‘
insert into #(myfd)
select ‘//****************************************datagrid列绑定*****************************************‘
insert into #(myfd)
select ‘dgModlist.Columns["cl‘+ name+‘"].DataPropertyName = "‘+name+‘";‘
from syscolumns a,information_schema.columns b
where a.name=b.COLUMN_NAME
and b.TABLE_NAME=@datamodel and object_name(id)=@datamodel
insert into #(myfd)
select ‘//****************************************form codebehind 新增*****************************************‘
declare @fbstr varchar(max) =‘‘
set @fbstr+=char(10)+‘public ProjectReg(int ID){‘
+char(10)+‘InitializeComponent();‘
+char(10)+‘_ID = ID;‘
+char(10)+‘if (_ID > 0)‘
+char(10)+‘InitView();‘
+char(10)+‘}‘
+char(10)+‘List<‘+@modelns+‘.‘+@clsname+‘> _Models; ‘
+char(10)+‘private void InitView(){‘
+char(10)+‘_Models = ‘+@modelns+‘.‘+@clsname+‘.PopObjectList(_ID, "", "");‘
+char(10)+‘if (_Models.Count > 0) {‘
+char(10)+@clsname+‘ model = _Models[0];‘
select @fbstr+= char(10)+ ‘_‘+name+‘=model.‘+name+‘;‘
from syscolumns a where object_name(id)=@datamodel
select @fbstr+=char(10)+‘}}‘
select @fbstr+=char(10)+‘string _‘+a.name+‘ { get { return txt‘+Name+‘.Text.Trim(); }set { txt‘+Name+‘.Text=value; } }‘
from syscolumns a where object_name(id)=@datamodel
set @fbstr+=char(10)+‘private void btnsave_Click(object sender, EventArgs e){‘
set @fbstr+=char(10)+@clsname +‘ model =new ‘+@clsname+‘();‘
select @fbstr+=char(10)+‘model.‘+name+‘=_‘+name+‘;‘
from syscolumns a where object_name(id)=@datamodel
set @fbstr+=char(10)+‘HandleMsg msg=‘+@clsname+‘.‘+@objname+‘Change(model);‘
set @fbstr+=char(10)+‘MessageBox.Show(msg.Msg);‘
set @fbstr+=char(10)+‘}‘
insert into #(myfd) values(@fbstr)
insert into #(myfd)
select ‘//****************************************‘+@clsname+‘*****************************************‘
declare @modelstr varchar(max)
select @modelstr=‘using System;‘
+char(10)+‘using System.Collections.Generic;‘
+char(10)+‘using System.Data; ‘
+char(10)+‘using System.Runtime.Serialization;‘
+char(10)+‘using System.Text;‘
select @modelstr+=char(10)+‘namespace ‘+@modelns+char(10)+‘{‘+char(10)+‘public class ‘+@clsname+‘‘+CHAR(10)+‘{‘
select @modelstr+=char(10)+‘public string Action{get;set;}‘
select @modelstr+=char(10)+‘public ‘+
case when b.data_type=‘datetime‘ then ‘string‘
when b.data_type=‘nvarchar‘ then ‘string‘
when b.data_type=‘varchar‘ then ‘string‘
when b.data_type=‘uniqueidentifier‘ then ‘string‘
when b.data_type=‘money‘ then ‘double‘
when b.data_type=‘bit‘ then ‘int‘
when b.DATA_TYPE=‘smallint‘ then ‘int‘
when b.DATA_TYPE=‘int‘ OR b.DATA_TYPE=‘bigint‘ then ‘int‘
when b.DATA_TYPE=‘decimal‘ then ‘double‘
else b.DATA_TYPE end+ ‘ ‘+
name+‘{get;set;}‘
+‘‘ from syscolumns a,information_schema.columns b
where a.name=b.COLUMN_NAME
and b.TABLE_NAME=@datamodel and object_name(id)=@datamodel
select @modelstr+=char(10)+‘public ‘+@clsname+‘() { }‘
select @modelstr+=char(10)+‘public ‘+@clsname+‘(DataRow dr) { ‘
select @modelstr+=char(10)+
case when b.data_type=‘datetime‘ OR b.data_type=‘date‘ then ‘if(!Convert.IsDBNull(dr["‘+name+‘"]))‘
+‘this.‘+name+‘= Convert.ToDateTime(dr["‘+name+‘"]).ToString("yyyy-MM-dd hh:mm:ss");‘
+‘else this.‘+name +‘="";‘
when b.data_type=‘nvarchar‘ or b.data_type=‘varchar‘ or b.data_type=‘uniqueidentifier‘ then ‘this.‘+name+‘=dr["‘+name+‘"].ToString();‘
when b.data_type=‘money‘ or b.DATA_TYPE=‘decimal‘ then
‘if (Convert.IsDBNull(dr["‘+name+‘"]))‘+char(10)+
‘this.‘+name+‘=0;‘+char(10)+
‘else ‘+
‘this.‘+name+‘=Convert.ToDouble(dr["‘+name+‘"]);‘
when b.data_type=‘bit‘ or b.DATA_TYPE=‘smallint‘ or b.DATA_TYPE=‘int‘ then
‘if (Convert.IsDBNull(dr["‘+name+‘"]))‘+char(10)+
‘this.‘+name+‘=0;‘+char(10)+
‘else ‘+
‘this.‘+name+‘=Convert.ToInt32(dr["‘+name+‘"]);‘
else ‘‘ end
from syscolumns a,information_schema.columns b
where a.name=b.COLUMN_NAME
and b.TABLE_NAME=@datamodel and object_name(id)=@datamodel
select @modelstr+=char(10)+‘this.Action="编 辑";‘
select @modelstr+=char(10)+‘}‘
+char(10)+‘public static HandleMsg ‘+@objname+‘Change(‘+@clsname+‘ model) {‘
+char(10)+‘Dictionary<string, object> dir = new Dictionary<string, object>();‘
select @modelstr+=char(10)+‘dir.Add("@‘+name +‘", model.‘+name+‘);‘
from syscolumns a where object_name(id)=@datamodel
select @modelstr+=char(10)+‘DataTable dt = ‘+@dpclsname+‘.‘+@objname+‘Change(dir);‘
+char(10)+‘HandleMsg msg = new HandleMsg(dt);‘
+char(10)+‘return msg;‘
+char(10)+‘}‘
select @modelstr+=char(10)+‘public static List<‘+@clsname+‘> PopObjectList(string parm, string userno)‘
+char(10)+‘{‘
+char(10)+‘Dictionary<string, object> dir = new Dictionary<string, object>();‘
+char(10)+‘dir.Add("@parm", parm);‘
+char(10)+‘dir.Add("@userno", userno);‘
+char(10)+‘DataTable dt = ‘+@dpclsname+‘.Get‘+@clsname+‘List(dir);‘+char(10)+
‘ List<‘+@clsname+‘> list = new List<‘+@clsname+‘>();‘+char(10)+
‘ foreach (DataRow dr in dt.Rows)‘+char(10)+
‘ {‘+char(10)+
‘ list.Add(new ‘+@clsname+‘(dr));‘+char(10)+
‘ }‘+char(10)+
‘ return list;‘+char(10)+
‘ }}}‘
insert into #(myfd)
select @modelstr
print @modelstr
insert into #(myfd)
select ‘//****************************************DATA Provider*****************************************‘
declare @dpstr varchar(max)=‘‘
select @dpstr+=char(10)+‘public static DataTable Get‘+@clsname+‘List(Dictionary<string, object> parms){‘
+char(10)+ ‘DataTable dt = _DataProvider.ExecStoreProcedureForGettingTable(parms, "‘+@readprocname+‘");‘
+char(10)+‘return dt;‘
+char(10)+‘} ‘+char(10)
select @dpstr+=char(10)+‘public static DataTable ‘+@objname+‘Change(Dictionary<string, object> parms){‘
select @dpstr+=char(10)+‘DataTable dt = _DataProvider.ExecStoreProcedureForGettingTable(parms, "‘+@writeprocname+‘");‘
select @dpstr+=char(10)+‘return dt;}‘
INSERT into #(myfd)values(@dpstr)
--**********************************输出WPF数据UI**************************************
SELECT --CAST(CASE WHEN ISNULL(g.[value],‘‘)=‘‘ THEN a.name ELSE g.[value] END AS VARCHAR) +CHAR(10)
‘<GridViewColumn Header="‘ + CAST(CASE WHEN ISNULL(g.[value],‘‘)=‘‘ THEN a.name ELSE g.[value] END AS VARCHAR)
+‘" Width="100" CellTemplateSelector="{x:Null}">‘+CHAR(10)
+‘<GridViewColumn.CellTemplate>‘+CHAR(10)
+‘<DataTemplate>‘+CHAR(10)
+‘<DockPanel LastChildFill="True">‘+CHAR(10)
+‘<Rectangle DockPanel.Dock="Right" Fill="#BABABE" Width="1" ></Rectangle>‘+CHAR(10)
+‘<TextBlock DockPanel.Dock="Left" Text="{Binding ID}" Width="100" HorizontalAlignment="Left"></TextBlock>‘+CHAR(10)
+‘</DockPanel>‘+CHAR(10)
+‘</DataTemplate>‘+CHAR(10)
+‘</GridViewColumn.CellTemplate>‘+CHAR(10)
+‘</GridViewColumn>‘+CHAR(10)
---- (case when a.colorder=1 then d.name else ‘‘ end) 表名,
FROM syscolumns a left join systypes b
on a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id and d.name<>‘dtproperties‘
left join sys.extended_properties g
on a.id=g.major_id AND a.colid = g.minor_id
WHERE d.[name] =@datamodel --你要查看的表名,注释掉,查看当前数据库所有表的字段信息
order by a.id,a.colorder
insert into #(myfd)values(‘
--***********************************************读取数据存储过程************************************************‘)
--***********************************************读取数据存储过程**************************************************
declare @fields varchar(max)=‘‘,@sqlstr varchar(max)=‘‘,@orderfields varchar(90)=‘‘
select @fields=stuff((select ‘,‘+name from syscolumns
where object_name(id)=‘‘+@datamodel+‘‘ order by colorder for xml path(‘‘)),1,1,‘‘)
select top 1 @orderfields=name from syscolumns where object_name(id)=‘‘+@datamodel+‘‘ order by colorder
set @sqlstr+=‘--‘+convert(varchar(10),getdate(),120)+‘‘
+char(10)+‘--读取‘+@modelname+‘‘
+char(10)+‘--‘+@readprocname+‘ @parm=‘‘‘‘‘
+char(10)+‘create PROC ‘+@readprocname+‘‘
+char(10)+‘@id INT=-1,‘
+char(10)+‘@parm nvarchar(90)=‘‘‘‘, ‘
+char(10)+‘@userno VARCHAR(20)=‘‘‘‘,‘
+char(10)+‘@isvalid INT =-1,‘
+char(10)+‘@sortfields nvarchar(100)=‘‘‘‘,‘
+char(10)+‘@pageindex int=1,‘
+char(10)+‘@pagesize int=500‘
+char(10)+‘AS‘
+char(10)+‘begin ‘
+char(10)+‘--******************************************数据准备**************************************‘
+char(10)+‘--*******************************************变量定义***************************************‘
+char(10)+‘--构建执行脚本‘
+char(10)+‘declare @sql nvarchar(1800)=‘‘‘‘,‘
+char(10)+‘--存储对象‘
+char(10)+‘@tablename NVARCHAR(50)=‘‘‘+@datamodel+‘‘‘,‘
+char(10)+‘--返回字段‘
+char(10)+‘@returnfields nvarchar(1000)=‘‘‘‘,‘
+char(10)+‘--where 条件‘
+char(10)+‘@where nvarchar(200)=‘‘ where 1=1‘‘,‘
+char(10)+‘--上次查询数量‘
+char(10)+‘@lastcount int =-1 ‘
+char(10)+‘--计算前面查询的数据总数‘
+char(10)+‘set @lastcount=(@pageindex-1)*@pagesize‘
+char(10)+‘if @lastcount<0 set @lastcount=0‘
+char(10)+‘--判断排序字段‘
+char(10)+‘if @sortfields=‘‘‘‘‘
+char(10)+‘set @sortfields=‘‘‘+@orderfields+‘‘‘‘
+char(10)+‘--*******************************************返回字段设定*****************************************‘
+char(10)+‘--select stuff((select ‘‘,‘‘+name from syscolumns ‘
+char(10)+‘--where object_name(id)=‘‘ext_AutoRunRegister‘‘ order by colorder for xml path(‘‘‘‘)),1,1,‘‘‘‘)‘
+char(10)+‘set @returnfields=‘‘‘+@fields+‘‘‘‘
+char(10)+‘--*******************************************由条件构建Where***************************************‘
+char(10)+‘if @id<>-1‘
+char(10)+‘set @where+=‘‘ and id =‘‘+CAST(@id AS VARCHAR)‘
+char(10)+‘if @userno<>‘‘‘‘‘
+char(10)+‘set @where+=‘‘ and userno like ‘‘‘‘%‘‘+@userno+‘‘%‘‘‘‘‘‘‘
+char(10)+‘--*******************************************由条件构建SQL***************************************‘
+char(10)+‘set @sql=‘‘‘
+char(10)+‘declare @lastmaxid int=0,@total int‘
+char(10)+‘--缓存的临时表,并创建检索rowID‘
+char(10)+‘if object_id(‘‘‘‘tempdb..#t‘‘‘‘) is not null‘
+char(10)+‘ drop table #t‘
+char(10)+‘select row_number() over (order by ‘‘+@sortfields+‘‘) rowid, * into #t from ‘‘+@tablename‘
+char(10)+‘set @sql += @where‘
+char(10)+‘--获取按照执行排序,前面查询的数据的最大ID‘
+char(10)+‘set @sql +=‘‘ select top ‘‘+cast(@lastcount as nvarchar)+‘‘ @lastmaxid=max(rowid) from #t‘‘‘
+char(10)+‘set @sql+= ‘‘ group by rowid‘‘‘
+char(10)+‘--获取符合条件的数据总数‘
+char(10)+‘set @sql+=‘‘ select @total=count(*) from #t‘‘‘
+char(10)+‘--**********************************************构造执行返回结果的SQL*****************************************‘
+char(10)+‘set @sql+=‘‘ select ‘‘‘
+char(10)+‘if @pagesize>0‘
+char(10)+‘ set @sql+=‘‘top ‘‘+cast(@pagesize as nvarchar) ‘
+char(10)+‘ set @sql+=‘‘ rowid ‘‘‘
+char(10)+‘ if @returnfields<>‘‘‘‘‘
+char(10)+‘ begin‘
+char(10)+‘ set @sql+=‘‘,‘‘+@returnfields ‘
+char(10)+‘ end ‘
+char(10)+‘ set @sql+=‘‘ from #t where rowid>@lastmaxid order by rowid‘‘‘
+char(10)+‘set @sql+=‘‘ select @total total,‘‘+cast(@pagesize as nvarchar)+‘‘ pagesize,‘‘+cast(@pageindex as nvarchar)+‘‘ pageindex‘‘‘
+char(10)+‘set @sql+=‘‘ if object_id(‘‘‘‘tempdb..#t‘‘‘‘) is not null‘
+char(10)+‘ drop table #t‘‘‘
+char(10)+‘set @sql+=‘‘ if object_id(‘‘‘‘tempdb..#x‘‘‘‘) is not null‘
+char(10)+‘ drop table #x‘‘‘
+char(10)+‘exec(@sql) ‘
+char(10)+‘END‘
insert into #(myfd)
select @sqlstr
insert into #(myfd)values(‘
--***********************************************写入数据存储过程************************************************‘)
--***********************************************写入数据存储过程**************************************************
declare
@yesUpdateCondition VARCHAR(300)=‘@id>0‘ --更新表时需要满足此条件
,@tbcols VARCHAR(2000)=‘‘
,@tbcolsp VARCHAR(2000)=‘‘
,@tbcolspv VARCHAR(2000)=‘‘
,@space VARCHAR(10)=‘ ‘
SELECT @tbcols= stuff((select ‘,‘+name from syscolumns
where object_name(id)=@datamodel order by colorder for xml path(‘‘)),1,1,‘‘)
SELECT @tbcolsp= stuff((select ‘,@‘+name from syscolumns
where object_name(id)=@datamodel order by colorder for xml path(‘‘)),1,1,‘‘)
SELECT @tbcolspv= stuff((select ‘,‘+name+‘=@‘+name from syscolumns
where object_name(id)=@datamodel order by colorder for xml path(‘‘)),1,1,‘‘)
create table #w(id INT IDENTITY(1,1),myfd varchar(500))
insert into #w(myfd)
SELECT ‘create proc ‘+@writeprocname
insert into #w(myfd)
SELECT ‘@‘+ a.name+‘ ‘+
CASE when b.data_type=‘datetime‘ OR b.data_type=‘date‘ then ‘datetime=‘‘1900-01-01‘‘‘
when b.data_type=‘nvarchar‘ OR b.data_type=‘varchar‘ then ‘nvarchar(‘+CAST(a.length AS VARCHAR)+‘)=‘‘‘‘‘
when b.data_type=‘uniqueidentifier‘ then ‘varchar(‘+CAST(a.length AS VARCHAR)+‘)=‘‘‘‘‘
when b.data_type=‘money‘ then ‘money=0‘
when b.data_type=‘bit‘ then ‘smallint=0‘
when b.DATA_TYPE=‘smallint‘ then ‘smallint=0‘
when b.DATA_TYPE=‘int‘ then ‘int=0‘
when b.DATA_TYPE=‘decimal‘ then ‘decimal(18,4)=0‘
else b.DATA_TYPE end+ ‘,‘
FROM syscolumns a,information_schema.columns b
where a.name=b.COLUMN_NAME
and b.TABLE_NAME=@datamodel and object_name(id)=@datamodel
UPDATE #w SET myfd=REPLACE(myfd,‘,‘,‘‘) WHERE id=@@ROWCOUNT+1
insert into #w(myfd)
select ‘as‘
insert into #w(myfd)
select ‘SET XACT_ABORT on‘
insert into #w(myfd)
select ‘begin tran‘
insert into #w(myfd)
select ‘ begin try‘
insert into #w(myfd)
select @space+‘ ‘+‘declare @count int,@msg varchar(200)=‘‘‘‘‘
INSERT INTO #w(myfd)
SELECT @space+‘ if(‘+@yesUpdateCondition+‘) ‘
INSERT INTO #w(myfd)
SELECT @space+‘ begin‘
INSERT INTO #w(myfd)
SELECT @space+@space+‘ update ‘+@datamodel+‘ set ‘+@tbcolspv
INSERT INTO #w(myfd)
SELECT @space+@space+‘ where ID=@ID‘
INSERT INTO #w(myfd)
SELECT @space+@space+‘ select @count=@@rowcount ‘
INSERT INTO #w(myfd)
SELECT @space+‘ end ‘
INSERT INTO #w(myfd)
SELECT @space+‘ else ‘
INSERT INTO #w(myfd)
SELECT @space+‘ begin ‘
INSERT INTO #w(myfd)
SELECT @space+@space+‘ insert into ‘+@datamodel+‘(‘+@tbcols+‘)‘
INSERT INTO #w(myfd)
SELECT @space+@space+‘ select ‘+@tbcolsp
INSERT INTO #w(myfd)
SELECT @space+@space+‘ select @count=@@rowcount ‘
INSERT INTO #w(myfd)
SELECT @space+‘ end ‘
INSERT INTO #w(myfd)
SELECT @space+‘ select @count tcount,case when @count>0 then ‘‘执行成功‘‘ else ‘‘执行失败‘‘ end msg ‘
INSERT INTO #w(myfd)
SELECT @space+‘ commit tran ‘
INSERT INTO #w(myfd)
SELECT ‘ end try‘
INSERT INTO #w(myfd)
SELECT ‘ begin catch ‘
INSERT INTO #w(myfd)
SELECT @space+‘ select @msg=error_message() ‘
INSERT INTO #w(myfd)
SELECT @space+‘ select 0 tcount,@msg msg‘
INSERT INTO #w(myfd)
SELECT @space+‘ rollback tran ‘
INSERT INTO #w(myfd)
SELECT ‘ end catch‘
select * from #
select * from #w
drop table #w
drop table #
机器Coding For WinForm