首页 > 代码库 > 机器Coding For WPF

机器Coding For WPF

declare @modelcode varchar(90)=‘AutoProjectType‘
declare @ns varchar(90)=‘WpfApplication‘
declare @datamodel varchar(90)=‘wv_‘ +@modelcode
declare @model varchar(90)=replace(@datamodel,‘wv_‘,‘‘)+‘Model‘
declare @modelFolder varchar(90)=‘PriceRequest‘
declare @viewmodelNamespace varchar(90)=‘‘
DECLARE @dataprovidercls VARCHAR(300)=‘BizModel.Provider.VMRDataProvider‘
DECLARE @modelname VARCHAR(300)=‘合作伙伴报价确认管理‘

declare @modelpath varchar(300)=‘‘
declare @viewmodelpath varchar(300)=‘‘
declare @dgviewpath varchar(300)=‘‘
declare @listviewpath varchar(300)=‘‘
declare @dataproviderpath varchar(300)=‘‘

declare @mid int

select @mid=projectid from UPDExt.[dbo].ext_AutoProjectModel where code=@modelcode
select @modelpath=xpath from UPDExt.[dbo].[ext_AutoProjectParts] where projectid=@mid and type=2
select @viewmodelpath=xpath from UPDExt.[dbo].[ext_AutoProjectParts] where projectid=@mid and type=4
select @dgviewpath=xpath from UPDExt.[dbo].[ext_AutoProjectParts] where projectid=@mid and type=7
select @listviewpath=xpath from UPDExt.[dbo].[ext_AutoProjectParts] where projectid=@mid and type=6
select @dataproviderpath=xpath from UPDExt.[dbo].[ext_AutoProjectParts] where projectid=@mid and type=3

set @viewmodelNamespace=@ns+‘.ViewModels.‘+@modelFolder

declare @out VARCHAR(MAX)=‘‘
--type 0:注释,1:访问方法,2:model,3:dataprovider,4:viewmodel,5:codebehind,6:ListViewUI,7:DatagridUI,8读存储过程,9写存储过程
create table #(myfd VARCHAR(MAX),type int default 0,xpath varchar(200) default ‘‘)
declare @procname varchar(90)=‘‘
set @procname=‘wp_getPager_‘+@modelcode+‘_ds‘

insert into #(myfd,type,xpath)
select ‘//****************************************class.cs*****************************************‘,0,‘‘
insert into #(myfd,type,xpath)
select ‘public List<‘+@model+‘> Get‘+@modelcode+‘List(string parm,string userno)‘+char(10)+
‘{‘+char(10)+
‘ return ‘+@model+‘.GetObjectList(parm,userno);‘+char(10)+
‘}‘+char(10)
,1,‘‘
insert into #(myfd)
select ‘//****************************************‘+@model+‘*****************************************‘

select
@out=‘using System;‘
+char(10)+‘using BizModel.Model;‘
+char(10)+‘using System.Collections.Generic;‘
+char(10)+‘using System.Data; ‘
+char(10)+‘using System.Runtime.Serialization;‘
+char(10)+‘using System.Text;‘
+char(10)+‘using System.Threading.Tasks;‘
+char(10)+ ‘namespace ‘+@viewmodelNamespace+char(10)+‘{‘+char(10)+‘public class ‘+@model+‘:BaseModel‘+CHAR(10)+‘{‘


select @out+=char(10)+ 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+‘;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{return _‘+name+‘;}set{base.SetField(ref _‘+name+‘,value);}}‘
+‘‘ from syscolumns a,information_schema.columns b
where a.name=b.COLUMN_NAME
and b.TABLE_NAME=@datamodel and object_name(id)=@datamodel


select @out+=char(10)+ ‘bool _GroupChecked=false; public bool GroupChecked { get { return _GroupChecked; } set { base.SetField(ref _GroupChecked, value); } }‘


select @out+=char(10)+‘public ‘+@model+‘() { }‘

select @out+=char(10)+‘public ‘+@model+‘(DataRow dr) { ‘

select @out+=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 @out+=char(10)+‘}‘+char(10)+
‘public static List<‘+@model+‘> PopObjectList(string parm, string userno)‘+char(10)+
‘{‘+char(10)+
‘ Dictionary<string,object> parms=new Dictionary<string,object>();‘+char(10)+
‘ parms.Add("@userno",userno);‘+char(10)+
‘ DataTable dt = ‘+@dataproviderCls+‘.Get‘+@model+‘List(parms);‘+char(10)+
‘ List<‘+@model+‘> list = new List<‘+@model+‘>();‘+char(10)+
‘ foreach (DataRow dr in dt.Rows)‘+char(10)+
‘ {‘+char(10)+
‘ list.Add(new ‘+@model+‘(dr));‘+char(10)+
‘ }‘+char(10)+
‘ return list;‘+char(10)+
‘ }}}‘
insert into #(myfd,type,xpath)
select @out,2, @modelpath
insert into #(myfd)
select ‘//****************************************DATA Provider*****************************************‘

INSERT into #(myfd,type,xpath)
select ‘internal static DataTable Get‘+@model+‘List(Dictionary<string, object> parms)‘+char(10)+
‘{‘+char(10)+
‘ DataTable dt = _Instance.ExecStoreProcedureForGettingTable(parms, "wp_getPager_‘+replace(@model,‘object‘,‘‘)+‘_ds");‘+char(10)+
‘ return dt;‘+char(10)+
‘} ‘+char(10)
,3,@dataproviderpath


insert into #(myfd)
select ‘//****************************************ViewModel*****************************************‘
DECLARE @str VARCHAR(MAX)

set @str=
‘using BizModel.Model;‘+CHAR(10)+
‘using MyGlobal.Infrustructure;‘+CHAR(10)+
‘using System;‘+CHAR(10)+
‘using System.Collections.Generic;‘+CHAR(10)+
‘using System.Diagnostics;‘+CHAR(10)+
‘using System.Linq;‘+CHAR(10)+
‘using System.Net;‘+CHAR(10)+
‘using System.Text;‘+CHAR(10)+
‘using System.Threading.Tasks;‘+CHAR(10)+
‘using System.Windows;‘+CHAR(10)+
‘using System.Windows.Input;‘+CHAR(10)+
‘using WpfApplication.Views;‘+CHAR(10)+
‘namespace WpfApplication.ViewModels.‘+@modelFolder+CHAR(10)+
‘{‘+CHAR(10)+
‘public class ‘+@modelcode+‘ViewModel‘+
‘ViewModel:NotifyChanged ‘+CHAR(10)+
‘{‘+CHAR(10)+
‘private List<‘+@model+‘> _mylist;‘+CHAR(10)+
‘public List<‘+@model+‘> MyListViewDS‘+CHAR(10)+
‘{‘+CHAR(10)+
‘get { return _mylist; }‘+CHAR(10)+
‘set { SetField(ref _mylist, value); }‘
+CHAR(10)+‘}‘

+CHAR(10)+‘public ICommand CHKSN_Command {‘
+CHAR(10)+‘get {‘
+CHAR(10)+‘return new RelayCommand<object>((o) => {‘
+CHAR(10)+‘foreach (PriceRequestReplyModel item in MyListViewDS)‘
+CHAR(10)+‘{‘
+CHAR(10)+‘ if (item.Apply_No == o.ToString()) item.GroupChecked = !item.GroupChecked;‘
+CHAR(10)+‘}}); } }‘
+CHAR(10)+‘public ICommand MyListView_PreviewMouseWheel‘
+CHAR(10)+‘{‘
+CHAR(10)+‘get‘
+CHAR(10)+‘{‘
+CHAR(10)+‘return new RelayCommand<ListView>((mylistview) =>‘
+CHAR(10)+‘{‘
+CHAR(10)+‘mylistview.PreviewMouseWheel += (sender, e) =>‘
+CHAR(10)+‘{‘
+CHAR(10)+‘ var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);‘
+CHAR(10)+‘ eventArg.RoutedEvent = UIElement.MouseWheelEvent;‘
+CHAR(10)+‘ eventArg.Source = sender;‘
+CHAR(10)+‘ mylistview.RaiseEvent(eventArg);‘
+CHAR(10)+‘};});}}‘


+CHAR(10)+‘public ICommand MyListView_SelectionChangedCommand {get{return new RelayCommand<object>((o) =>{ });}}‘

+CHAR(10)+‘public ICommand BtnQueryCommand‘+CHAR(10)+
‘{‘+CHAR(10)+
‘get‘+CHAR(10)+
‘{‘+CHAR(10)+
‘return new RelayCommand<string>(‘+CHAR(10)+
‘(o) =>‘+CHAR(10)+
‘{‘+CHAR(10)+
‘MyListViewDS = ‘+@model+‘.GetObjectList("","");‘+CHAR(10)+
‘});‘+CHAR(10)+
‘}‘+CHAR(10)+
‘}‘+CHAR(10)+
‘}‘+CHAR(10)+
‘}‘+CHAR(10)

PRINT @str
BEGIN TRY
INSERT INTO #(myfd,type,xpath)
SELECT @str,4,@viewmodelpath
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
END CATCH

 

insert into #(myfd)
select ‘//****************************************View behindcode*****************************************‘
INSERT INTO #(myfd,type,xpath)
SELECT
‘using MyGlobal.Infrustructure.Resources;‘+CHAR(10)+
‘using System;‘+CHAR(10)+
‘using System.Collections.Generic;‘+CHAR(10)+
‘using System.Linq;‘+CHAR(10)+
‘using System.Text;‘+CHAR(10)+
‘using System.Threading.Tasks;‘+CHAR(10)+
‘using System.Windows;‘+CHAR(10)+
‘using System.Windows.Controls;‘+CHAR(10)+
‘using System.Windows.Data;‘+CHAR(10)+
‘using System.Windows.Documents;‘+CHAR(10)+
‘using System.Windows.Input;‘+CHAR(10)+
‘using System.Windows.Media;‘+CHAR(10)+
‘using System.Windows.Media.Imaging;‘+CHAR(10)+
‘using System.Windows.Navigation;‘+CHAR(10)+
‘using System.Windows.Shapes;‘+CHAR(10)+
‘using WpfApplication.ViewModels.‘+@modelFolder+‘;‘+char(10)+
‘namespace WpfApplication.Views.‘+@modelFolder+char(10)+
‘{‘+char(10)+
‘public partial class ‘+@modelcode+‘View : Page‘+char(10)+
‘{‘+char(10)+
‘public ‘+@modelcode+‘View()‘+char(10)+
‘{‘+char(10)+
‘//*************************************引用全局单例样式代码******************************‘+char(10)+
‘this.Resources.MergedDictionaries.Add(SharedDictionaryStyleManager.SharedStylesDictionary);‘+char(10)+
‘this.Resources.MergedDictionaries.Add(SharedDictionaryStyleManager.ShareDataGridStylesDictionary);‘+char(10)+
‘this.DataContext = new ‘+@modelcode+‘ViewModel();‘+char(10)+
‘InitializeComponent();‘+char(10)+
‘}‘+char(10)+
‘}‘+char(10)+
‘}‘+char(10)
,5,@dgviewpath

INSERT INTO #(myfd)
VALUES(‘--**********************************输出WPF数据UI For ListView**************************************‘)
DECLARE @str1 VARCHAR(MAX) =‘‘
SELECT @str1+=CHAR(10)+
‘<Page x:Class="WpfApplication.Views.‘+@modelFolder+‘.‘+@modelcode+‘View"‘+CHAR(10)
+‘xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"‘+CHAR(10)
+‘xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"‘+CHAR(10)
+‘xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" ‘+CHAR(10)
+‘xmlns:d="http://schemas.microsoft.com/expression/blend/2008" ‘+CHAR(10)
+‘xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" ‘+CHAR(10)

+‘mc:Ignorable="d" ‘+CHAR(10)
+‘d:DesignHeight="500" d:DesignWidth="790"‘+CHAR(10)
+‘Title="‘+@modelname+‘">‘+CHAR(10)

+CHAR(10)+‘<Page.Resources>‘
+CHAR(10)+‘<CollectionViewSource x:Key=‘‘src‘‘ Source="{Binding MyListViewDS}">‘
+CHAR(10)+‘<CollectionViewSource.GroupDescriptions>‘
+CHAR(10)+‘<PropertyGroupDescription PropertyName="Apply_No" />‘
+CHAR(10)+‘</CollectionViewSource.GroupDescriptions>‘
+CHAR(10)+‘</CollectionViewSource>‘
+CHAR(10)+‘<!--</SnippetGroupingCollectionViewSource>-->‘
+CHAR(10)+‘<Style TargetType="{x:Type ListViewItem}">‘
+CHAR(10)+‘<Setter Property="BorderThickness" Value="http://www.mamicode.com/0,1,0,0" />‘
+CHAR(10)+‘<Setter Property="BorderBrush" Value="http://www.mamicode.com/#BABABE" />‘
+CHAR(10)+‘</Style>‘
+CHAR(10)+‘</Page.Resources>‘

+‘<Grid>‘+CHAR(10)
+‘<Grid.RowDefinitions>‘+CHAR(10)
+‘<RowDefinition Height="40"></RowDefinition>‘+CHAR(10)
+‘<RowDefinition Height="10"></RowDefinition>‘+CHAR(10)
+‘<RowDefinition Height="25"></RowDefinition>‘+CHAR(10)
+‘<RowDefinition Height="10"></RowDefinition>‘+CHAR(10)
+‘<RowDefinition></RowDefinition>‘+CHAR(10)
+‘</Grid.RowDefinitions>‘+CHAR(10)
+‘<Grid.ColumnDefinitions>‘+CHAR(10)
+‘<ColumnDefinition Width="*"></ColumnDefinition>‘+CHAR(10)
+‘<ColumnDefinition Width="2*"></ColumnDefinition>‘+CHAR(10)
+‘<ColumnDefinition Width="*"></ColumnDefinition>‘+CHAR(10)
+‘<ColumnDefinition Width="2*"></ColumnDefinition>‘+CHAR(10)
+‘<ColumnDefinition Width="*"></ColumnDefinition>‘+CHAR(10)
+‘</Grid.ColumnDefinitions>‘+CHAR(10)

+‘<Border Style="{StaticResource csTitleBorder}" Grid.ColumnSpan="5" >‘+CHAR(10)
+‘<StackPanel Background="{StaticResource gbTitleBg}">‘+CHAR(10)
+‘<TextBlock Text="‘+@modelname+‘" VerticalAlignment="Center" Style="{StaticResource gbTextBlock}"></TextBlock>‘
+CHAR(10)+‘</StackPanel>‘
+CHAR(10)+‘</Border>‘
+CHAR(10)+‘<Label Grid.Row="2" Style="{StaticResource csLabel}">姓名</Label>‘
+CHAR(10)+‘<TextBox Text="{Binding SearchName}" Grid.Row="2" Grid.Column="1" VerticalContentAlignment="Center" Name="fefe"></TextBox>‘
+CHAR(10)+‘<Label Grid.Row="2" Grid.Column="2" Style="{StaticResource csLabel}">工号</Label>‘
+CHAR(10)+‘<TextBox Text="{Binding SearchNO}" Grid.Row="2" Grid.Column="3" VerticalContentAlignment="Center"></TextBox>‘
+CHAR(10)+‘<Button Grid.Row="2" Grid.Column="4" Command="{Binding DataContext.QueryUsersCommand,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Page}}}" >查询</Button>‘
+CHAR(10)+‘<ListView Name="MyListView" Grid.Row="4" Grid.ColumnSpan="5" ItemsSource=‘‘{Binding Source={StaticResource src}}‘‘ BorderThickness="0">‘

+CHAR(10)+‘<i:Interaction.Triggers>‘
+CHAR(10)+‘<i:EventTrigger EventName="PreviewMouseWheel">‘
+CHAR(10)+‘<i:InvokeCommandAction Command="{Binding DataContext.MyListView_PreviewMouseWheel, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListView}}" CommandParameter="{Binding ElementName=MyListView}" >‘
+CHAR(10)+‘</i:InvokeCommandAction>‘
+CHAR(10)+‘</i:EventTrigger>‘
+CHAR(10)+‘<i:EventTrigger EventName="SelectionChanged">‘
+CHAR(10)+‘<i:InvokeCommandAction Command="{Binding DataContext.MyListView_SelectionChangedCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListView}}" CommandParameter="{Binding}" >‘
+CHAR(10)+‘</i:InvokeCommandAction>‘
+CHAR(10)+‘</i:EventTrigger>‘
+CHAR(10)+‘</i:Interaction.Triggers>‘


+CHAR(10)+‘<ListView.GroupStyle>‘
+CHAR(10)+‘<GroupStyle>‘
+CHAR(10)+‘<GroupStyle.ContainerStyle>‘
+CHAR(10)+‘<Style TargetType="{x:Type GroupItem}">‘
+CHAR(10)+‘<Setter Property="Margin" Value="http://www.mamicode.com/0,0,0,5"/>‘
+CHAR(10)+‘<Setter Property="Template">‘
+CHAR(10)+‘<Setter.Value>‘
+CHAR(10)+‘<ControlTemplate TargetType="{x:Type GroupItem}">‘
+CHAR(10)+‘<Border BorderBrush="Blue" BorderThickness="0.5" CornerRadius="3,3,3,3" Margin="3,3,3,3">‘
+CHAR(10)+‘<Expander IsExpanded="True" BorderBrush="Blue" BorderThickness="0,0,0,0">‘
+CHAR(10)+‘<Expander.Header>‘
+CHAR(10)+‘<DockPanel>‘
+CHAR(10)+‘<Rectangle DockPanel.Dock="Bottom" Fill="Black" Height="1" ></Rectangle>‘

+CHAR(10)+‘<CheckBox Name="CHKSN">‘
+CHAR(10)+‘<i:Interaction.Triggers>‘
+CHAR(10)+‘<i:EventTrigger EventName="Click">‘
+CHAR(10)+‘<i:InvokeCommandAction Command="{Binding DataContext.CHKSN_Command, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListView}}" CommandParameter="{Binding Name}" >‘
+CHAR(10)+‘</i:InvokeCommandAction>‘
+CHAR(10)+‘</i:EventTrigger>‘
+CHAR(10)+‘</i:Interaction.Triggers>‘
+CHAR(10)+‘</CheckBox>‘

+CHAR(10)+‘<TextBlock FontWeight="Bold" Foreground="Blue" Text="{Binding Path=Name,StringFormat=‘‘SN:\{0\}‘‘}" Margin="5,0,0,0" Width="150"/>‘
+CHAR(10)+‘<TextBlock FontWeight="Bold" Foreground="Blue" Text="{Binding Path=ItemCount,StringFormat=‘‘数量:\{0\}‘‘}"/>‘


+CHAR(10)+‘</DockPanel>‘
+CHAR(10)+‘</Expander.Header>‘
+CHAR(10)+‘<Expander.Content>‘
+CHAR(10)+‘<ItemsPresenter />‘
+CHAR(10)+‘</Expander.Content>‘
+CHAR(10)+‘</Expander>‘
+CHAR(10)+‘</Border>‘
+CHAR(10)+‘</ControlTemplate>‘
+CHAR(10)+‘</Setter.Value>‘
+CHAR(10)+‘</Setter>‘
+CHAR(10)+‘</Style>‘
+CHAR(10)+‘</GroupStyle.ContainerStyle>‘
+CHAR(10)+‘</GroupStyle>‘
+CHAR(10)+‘</ListView.GroupStyle>‘
+CHAR(10)+‘<!--</SnippetListViewGroups>-->‘
+CHAR(10)+‘<ListView.View>‘
+CHAR(10)+‘<GridView>‘

SELECT @str1+=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 ‘+a.name+‘}" 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

SET @str1+=char(10)
+‘</GridView>‘
+‘</ListView.View>‘
+‘<!--<SnippetListViewEnd>-->‘
+‘</ListView>‘
+‘<!--</SnippetListViewEnd>-->‘
+‘</Grid></Page>‘
INSERT INTO #(myfd,type,xpath)
SELECT @str1,6,@dgviewpath

 

INSERT INTO #(myfd)
VALUES(‘--**********************************输出WPF数据UI For DataGrid**************************************‘)

SELECT @str1=
‘<Page x:Class="WpfApplication.Views.‘+@modelFolder+‘.‘+@modelcode+‘View"‘+CHAR(10)
+‘xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"‘+CHAR(10)
+‘xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"‘+CHAR(10)
+‘xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" ‘+CHAR(10)
+‘xmlns:d="http://schemas.microsoft.com/expression/blend/2008" ‘+CHAR(10)
+‘xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" ‘+CHAR(10)

+‘mc:Ignorable="d" ‘+CHAR(10)
+‘d:DesignHeight="500" d:DesignWidth="790"‘+CHAR(10)
+‘Title="‘+@modelname+‘">‘
+CHAR(10)+‘<Page.Resources>‘
+CHAR(10)+‘<CollectionViewSource x:Key=‘‘src‘‘ Source="{Binding MyListViewDS}">‘
+CHAR(10)+‘<CollectionViewSource.GroupDescriptions>‘
+CHAR(10)+‘<PropertyGroupDescription PropertyName="Apply_No" />‘
+CHAR(10)+‘</CollectionViewSource.GroupDescriptions>‘
+CHAR(10)+‘</CollectionViewSource>‘
+CHAR(10)+‘<!--</SnippetGroupingCollectionViewSource>-->‘
+CHAR(10)+‘<Style TargetType="{x:Type ListViewItem}">‘
+CHAR(10)+‘<Setter Property="BorderThickness" Value="http://www.mamicode.com/0,1,0,0" />‘
+CHAR(10)+‘<Setter Property="BorderBrush" Value="http://www.mamicode.com/#BABABE" />‘
+CHAR(10)+‘</Style>‘
+CHAR(10)+‘</Page.Resources>‘

+‘<Grid>‘+CHAR(10)
+‘<Grid.RowDefinitions>‘+CHAR(10)
+‘<RowDefinition Height="40"></RowDefinition>‘+CHAR(10)
+‘<RowDefinition Height="10"></RowDefinition>‘+CHAR(10)
+‘<RowDefinition Height="25"></RowDefinition>‘+CHAR(10)
+‘<RowDefinition Height="10"></RowDefinition>‘+CHAR(10)
+‘<RowDefinition></RowDefinition>‘+CHAR(10)
+‘</Grid.RowDefinitions>‘+CHAR(10)
+‘<Grid.ColumnDefinitions>‘+CHAR(10)
+‘<ColumnDefinition Width="*"></ColumnDefinition>‘+CHAR(10)
+‘<ColumnDefinition Width="2*"></ColumnDefinition>‘+CHAR(10)
+‘<ColumnDefinition Width="*"></ColumnDefinition>‘+CHAR(10)
+‘<ColumnDefinition Width="2*"></ColumnDefinition>‘+CHAR(10)
+‘<ColumnDefinition Width="*"></ColumnDefinition>‘+CHAR(10)
+‘</Grid.ColumnDefinitions>‘+CHAR(10)

+‘<Border Style="{StaticResource csTitleBorder}" Grid.ColumnSpan="5" >‘+CHAR(10)
+‘<StackPanel Background="{StaticResource gbTitleBg}">‘+CHAR(10)
+‘<TextBlock Text="‘+@modelname+‘" VerticalAlignment="Center" Style="{StaticResource gbTextBlock}"></TextBlock>‘
+CHAR(10)+‘</StackPanel>‘
+CHAR(10)+‘</Border>‘
+CHAR(10)+‘<Label Grid.Row="2" Style="{StaticResource csLabel}">姓名</Label>‘
+CHAR(10)+‘<TextBox Text="{Binding SearchName}" Grid.Row="2" Grid.Column="1" VerticalContentAlignment="Center" Name="fefe"></TextBox>‘
+CHAR(10)+‘<Label Grid.Row="2" Grid.Column="2" Style="{StaticResource csLabel}">工号</Label>‘
+CHAR(10)+‘<TextBox Text="{Binding SearchNO}" Grid.Row="2" Grid.Column="3" VerticalContentAlignment="Center"></TextBox>‘
+CHAR(10)+‘<Button Grid.Row="2" Grid.Column="4" Command="{Binding DataContext.QueryUsersCommand,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Page}}}" >查询</Button>‘


+CHAR(10)+‘<DataGrid Name="MyListView" Grid.Row="4" AutoGenerateColumns="False" Grid.ColumnSpan="5" ItemsSource=‘‘{Binding Source={StaticResource src}}‘‘ CanUserAddRows="False" BorderThickness="0">‘
+CHAR(10)+‘<i:Interaction.Triggers>‘
+CHAR(10)+‘<i:EventTrigger EventName="SelectionChanged">‘
+CHAR(10)+‘<i:InvokeCommandAction Command="{Binding DataContext.MyListView_SelectionChangedCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" CommandParameter="{Binding}" >‘
+CHAR(10)+‘</i:InvokeCommandAction>‘
+CHAR(10)+‘</i:EventTrigger>‘
+CHAR(10)+‘</i:Interaction.Triggers>‘

+CHAR(10)+‘<DataGrid.GroupStyle>‘
+CHAR(10)+‘<GroupStyle>‘
+CHAR(10)+‘<GroupStyle.ContainerStyle>‘
+CHAR(10)+‘<Style TargetType="{x:Type GroupItem}">‘
+CHAR(10)+‘<Setter Property="Margin" Value="http://www.mamicode.com/0,0,0,5"/>‘
+CHAR(10)+‘<Setter Property="Template">‘
+CHAR(10)+‘<Setter.Value>‘
+CHAR(10)+‘<ControlTemplate TargetType="{x:Type GroupItem}">‘
+CHAR(10)+‘<Border BorderBrush="Blue" BorderThickness="0.5" CornerRadius="3,3,3,3" Margin="3,3,3,3">‘
+CHAR(10)+‘<Expander IsExpanded="True" BorderBrush="Blue" BorderThickness="0,0,0,0">‘
+CHAR(10)+‘<Expander.Header>‘
+CHAR(10)+‘<DockPanel>‘
+CHAR(10)+‘<Rectangle DockPanel.Dock="Bottom" Fill="Black" Height="1" ></Rectangle>‘

+CHAR(10)+‘<CheckBox Name="CHKSN">‘
+CHAR(10)+‘<i:Interaction.Triggers>‘
+CHAR(10)+‘<i:EventTrigger EventName="Click">‘
+CHAR(10)+‘<i:InvokeCommandAction Command="{Binding DataContext.CHKSN_Command, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" CommandParameter="{Binding Name}" >‘
+CHAR(10)+‘</i:InvokeCommandAction>‘
+CHAR(10)+‘</i:EventTrigger>‘
+CHAR(10)+‘</i:Interaction.Triggers>‘
+CHAR(10)+‘</CheckBox>‘

+CHAR(10)+‘<TextBlock FontWeight="Bold" Foreground="Blue" Text="{Binding Path=Name,StringFormat=‘‘SN:\{0\}‘‘}" Margin="5,0,0,0" Width="150"/>‘
+CHAR(10)+‘<TextBlock FontWeight="Bold" Foreground="Blue" Text="{Binding Path=ItemCount,StringFormat=‘‘数量:\{0\}‘‘}"/>‘


+CHAR(10)+‘</DockPanel>‘
+CHAR(10)+‘</Expander.Header>‘
+CHAR(10)+‘<Expander.Content>‘
+CHAR(10)+‘<ItemsPresenter />‘
+CHAR(10)+‘</Expander.Content>‘
+CHAR(10)+‘</Expander>‘
+CHAR(10)+‘</Border>‘
+CHAR(10)+‘</ControlTemplate>‘
+CHAR(10)+‘</Setter.Value>‘
+CHAR(10)+‘</Setter>‘
+CHAR(10)+‘</Style>‘
+CHAR(10)+‘</GroupStyle.ContainerStyle>‘
+CHAR(10)+‘</GroupStyle>‘
+CHAR(10)+‘</DataGrid.GroupStyle>‘
+CHAR(10)+‘<DataGrid.Columns>‘

SELECT @str1+=char(10)
+‘<DataGridTemplateColumn Header="‘ + CAST(CASE WHEN ISNULL(g.[value],‘‘)=‘‘ THEN a.name ELSE g.[value] END AS VARCHAR)
+‘" Width="100" >‘+CHAR(10)
+‘<DataGridTemplateColumn.CellTemplate>‘+CHAR(10)
+‘<DataTemplate>‘+CHAR(10)
+‘<DockPanel LastChildFill="True">‘+CHAR(10)
+‘<Border BorderThickness="0,0.5,0.5,0" BorderBrush="#BABABE">‘+CHAR(10)
+‘<TextBlock DockPanel.Dock="Left" Text="{Binding ‘+a.name+‘}" Width="100" HorizontalAlignment="Left"></TextBlock></Border>‘+CHAR(10)
+‘</DockPanel>‘+CHAR(10)
+‘</DataTemplate>‘+CHAR(10)
+‘</DataGridTemplateColumn.CellTemplate>‘+CHAR(10)
+‘</DataGridTemplateColumn>‘+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

SET @str1+=char(10)
+‘ </DataGrid.Columns>‘
+‘</DataGrid>‘
+‘</Grid></Page>‘
INSERT INTO #(myfd,type,xpath)
SELECT @str1,7,@listviewpath

 

 


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)+‘--‘+@procname+‘ @parm=‘‘‘‘‘
+char(10)+‘create PROC ‘+@procname+‘‘
+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,type,xpath)
select @sqlstr,8,‘‘

 

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(max))
insert into #w(myfd)
SELECT ‘create proc wp_‘+@modelcode+‘_change‘
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 1=1‘
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 WPF