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="0,1,0,0" />'
+CHAR(10)+'<Setter Property="BorderBrush" Value="#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="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="0,1,0,0" />'
+CHAR(10)+'<Setter Property="BorderBrush" Value="#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="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的更多相关文章

  1. 机器Coding For WinForm

    declare @objname varchar(90)='AutoProjectModel'declare @modelns varchar(90)='ACL.Models'declare @dpc ...

  2. 【重榜?】.NET 6 Preview 1 开箱上手!带你尝试新版本更新!

    目录 跨平台 UI 应用 Blazor 桌面应用 System.CommandLine 其它更新 ASP.NET Core 最近 .NET 6 Preview 1 发布了,.NET 统一是此版本的核心 ...

  3. Coding theano under remote ubuntu server from local Mac (在本地mac机器上,写、跑、调试、看-远程ubuntu上的theano代码)

    本人是奇葩,最近鼓捣了一套在mac上coding远程ubuntu上的theano代码的东东,记之以期造福后人. Overview: 下图是我的编程环境和网络环境 我期望能在本地mac机器上对远程的ub ...

  4. WPF MVVM UI分离之《交互与数据分离》 基础才是重中之重~delegate里的Invoke和BeginInvoke 将不确定变为确定系列~目录(“机器最能证明一切”) 爱上MVC3系列~全局异常处理与异常日志 基础才是重中之重~lock和monitor的区别 将不确定变成确定~我想监视我的对象,如果是某个值,就叫另一些方法自动运行 将不确定变成确定~LINQ DBML模型可以对

    WPF MVVM UI分离之<交互与数据分离>   在我们使用WPF过程中,不可避免并且超级喜欢使用MVVM框架. 那么,使用MVVM的出发点是视觉与业务逻辑分离,即UI与数据分离 诸如下 ...

  5. 搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 (1)

    搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 原文地址(英文):http://www.networkcomms.net/creating ...

  6. 2000条你应知的WPF小姿势 基础篇<28-33 WPF启动故事>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  7. 2000条你应知的WPF小姿势 基础篇<15-21>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师,对C#和WPF有着极深的热情.最为出色的是他维护了两个博客:2,000Things You Should Know ...

  8. 工欲善其事,必先利其器 之 WPF篇: 随着开发轨迹来看高效WPF开发的工具和技巧

    之前一篇<工欲善其事,必先利其器.VS2013全攻略(安装,技巧,快捷键,插件)!> 看到很多朋友回复和支持,非常感谢,尤其是一些拍砖的喷油,感谢你们的批评,受益良多. 我第一份工作便是W ...

  9. [bigdata] 使用Redis队列来实现与机器无关的Job提交与执行 (python实现)

    用例场景: 定时从远程多台机器上下载文件存入HDFS中.一开始采用shell 一对一的方式实现,但对于由于网络或者其他原因造成下载失败的任务无法进行重试,且如果某台agent机器down机,将导致它对 ...

随机推荐

  1. Java知多少(62)线程同步

    当两个或两个以上的线程需要共享资源,它们需要某种方法来确定资源在某一刻仅被一个线程占用.达到此目的的过程叫做同步(synchronization).像你所看到的,Java为此提供了独特的,语言水平上的 ...

  2. 使用Qt Creator进行linux远程调试

    序言 考虑到程序员在无桌面Linux的环境下编写C/C++程序,如果使用原生的Vim编辑和单步断点调试比较麻烦,不利于新手使用,所以笔者尝试使用Qt实现两台Linux电脑远程部署和调试.即程序员在调试 ...

  3. Mybatis使用MySQL模糊查询时输入中文检索不到结果怎么办--转自http://www.jb51.net/article/88236.htm

    这篇文章主要介绍了Mybatis使用MySQL模糊查询时输入中文检索不到结果的解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下   项目开发中,在做Mybatis动态查询时,遇到了 ...

  4. 【QT】QT下载与安装

    很简单 1.下载地址 http://download.qt.io/archive/qt/ 学个单词,archive. 选择一个版本下载,5.9.3. 2.安装 选择组件 MinGW就行了,MinGW- ...

  5. python使用requests发送multipart/form-data请求数据

    def client_post_mutipart_formdata_requests(request_url,requestdict): #功能说明:发送以多部分表单数据格式(它要求post的消息体分 ...

  6. WebApi中的Session与Token间的处理对接

    首先,说起来创建session,一般会针对注册登录或者授权等情况: session 从字面上讲,就是会话.这个就类似于你和一个人交谈,你怎么知道当前和你交谈的是张三而不是李四呢?对方肯定有某种特征(长 ...

  7. Spring.NET依赖注入框架学习--简单对象注入

    Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...

  8. 整型数字转utf8

    static std::string codePointToUTF8(unsigned int cp){ std::string result; // based on description fro ...

  9. python 同时运行两个程序

    linux的话: python py1.py & python py2/py &

  10. MySQL的JDBC驱动源码解析

    原文:   MySQL的JDBC驱动源码解析 大家都知道JDBC是Java访问数据库的一套规范,具体访问数据库的细节有各个数据库厂商自己实现 Java数据库连接(JDBC)由一组用 Java 编程语言 ...