2016年9月

首页2016年9月
28
Sep
0

关闭所有触发器

exec sp_msforeachtable 'ALTER TABLE ? DISABLE TRIGGER all'
go

--打开部分触发器
ALTER TABLE T_CONSUME ENABLE TRIGGER T_CONSUME_DELETE
GO
ALTER TABLE T_CONSUME ENABLE TRIGGER T_CONSUME_INSERT
GO
ALTER TABLE T_CONSUME ENABLE TRIGGER T_CONSUME_UPDATE
GO
ALTER TABLE T_ATTENDANCE ENABLE TRIGGER ATTENDANCE_ADD
GO

18
Sep
0

WPF MVVM 事件绑定

在使用MVVM时经常用到Command,鼠标双击,单击都可用Command绑上

把事件绑定到Command上通常有3种:

1.使用MouseBinding

2.引用System.Windows.Interactivity.dll(必须装了Blend才有该组件),使用触发器来绑定

3.使用CommandBehavior附加属性绑定(需下载CommandBehavior源码)

下面来看看这3种方法的优劣

0.png

使用MouseBinding:

WPF MVVM 事件绑定 - 唐 - Doncle
但是MouseAction只有这些成员:
WPF MVVM 事件绑定 - 唐 - Doncle
事件成员很基本,但是在实际应用上算少的了 ,而且不能在样式(Setter)上使用

使用触发器
1.png
使用前加命名空间 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

WPF MVVM 事件绑定 - 唐 - Doncle
虽然可以使用EventName指定事件,所有事件都能涵括到,但是也不能在样式上使用

使用CommandBehavior

WPF MVVM 事件绑定 - 唐 - Doncle
这种方法通过使用附加属性、反射及动态方法,劣势当然是效率低啦

6599280884540231168.png

CommandBehavior源码摘自:AttachedCommandBehavior aka ACB

AttachedCommandBehavior.zip

关于集合的绑定:AttachedCommandBehavior V2 aka ACB

AttachedCommandBehavior v2.0.zip!

18
Sep
0

备份数据库批处理

@ECHO ON
set d=%date:~0,10%
set d=%d:-=%
set t=%time:~0,8%
set t=%t::=%
set stamp=%p%%d%%t%
set bakupfolder=f:sqlbackup\
rem 1按子目录保存备份文件;0不按
set lay_in_subfolder=1

call :backupone HDWMSSOEDB

goto :EOF

@ECHO OFF

:backupone
setlocal
echo %1
set dbname=%1
if not exist %bakupfolder%%dbname% mkdir %bakupfolder%%dbname%

if %lay_in_subfolder%==1 (
set subfolder=%dbname%\
)else set subfolder=
rem echo %bakupfolder%%subfolder%%dbname%%stamp%.bak
sqlcmd -U sa -P "SOEsoft123" -S localhost -Q "backup database %dbname% to disk='%bakupfolder%%subfolder%%dbname%%stamp%.bak'"
"C:Program FilesWinRARRAR.exe" a -ep1 -r -o+ -m5 -s -df "%bakupfolder%%subfolder%%dbname%%stamp%".rar "%bakupfolder%%subfolder%%dbname%%stamp%.bak"
endlocal&goto :EOF

14
Sep
0

数据库备份,日志收缩代码

//备份
s := 'backup database 大浪淘沙HDWMSSOEDB20160913 to disk=''d:数据备份sql.bak''';
//收缩日志
s := 'ALTER DATABASE [大浪淘沙HDWMSSOEDB20160913] SET RECOVERY SIMPLE;' +

   'DBCC SHRINKFILE (''HDWMSDB_log'',9,TRUNCATEONLY);' +
   'ALTER DATABASE [大浪淘沙HDWMSSOEDB20160913] SET RECOVERY FULL WITH NO_WAIT;'+
   'ALTER DATABASE [大浪淘沙HDWMSSOEDB20160913] SET RECOVERY FULL;';

sqExecSQL.SQL.Text := s;
sqExecSQL.ExecSQL;

02
Sep
0

WPF MVVM DATAGRID单双击事件

事件处理关联类

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace XXX.Module.ShopManager.Helpers
{
    //--helpers:RowDoubleClickHandler.MethodName="Edit"
    public sealed class RowDoubleClickHandler : FrameworkElement
    {
        public RowDoubleClickHandler(DataGrid dataGrid)
        {
            MouseButtonEventHandler handler = (sender, args) =>
            {
                var row = sender as DataGridRow;
                if (row != null && row.IsSelected)
                {
                    var methodName = GetMethodName(dataGrid);

                    var dataContextType = dataGrid.DataContext.GetType();
                    var method = dataContextType.GetMethod(methodName);
                    if (method == null)
                    {
                        throw new MissingMethodException(methodName);
                    }

                    method.Invoke(dataGrid.DataContext, null);
                }
            };

            dataGrid.LoadingRow += (s, e) =>
            {
                e.Row.MouseDoubleClick += handler;
            };

            dataGrid.UnloadingRow += (s, e) =>
            {
                e.Row.MouseDoubleClick -= handler;
            };
        }
 
        public static string GetMethodName(DataGrid dataGrid)
        {
            return (string)dataGrid.GetValue(MethodNameProperty);
        }

        public static void SetMethodName(DataGrid dataGrid, string value)
        {
            dataGrid.SetValue(MethodNameProperty, value);
        }

        public static readonly DependencyProperty MethodNameProperty = DependencyProperty.RegisterAttached(
            "MethodName",
            typeof(string),
            typeof(RowDoubleClickHandler),
            new PropertyMetadata((o, e) =>
            {
                var dataGrid = o as DataGrid;
                if (dataGrid != null)
                {
                    new RowDoubleClickHandler(dataGrid);
                }
            }));

    }
    //=======================================================================
    public sealed class RowClickHandler : FrameworkElement
    {
      public RowClickHandler(DataGrid dataGrid)
        {
            //RoutedEventHandler
            MouseButtonEventHandler handler = (sender, args) =>
            {
                var row = sender as DataGridRow;
                if (row != null && row.IsSelected)
                {
                    var methodName = GetMethodName(dataGrid);

                    var dataContextType = dataGrid.DataContext.GetType();
                    var method = dataContextType.GetMethod(methodName);
                    if (method == null)
                    {
                        throw new MissingMethodException(methodName);
                    }

                    method.Invoke(dataGrid.DataContext, null);
                }
            };

            dataGrid.LoadingRow += (s, e) =>
            {  
                e.Row.MouseLeftButtonUp += handler;
            };

            dataGrid.UnloadingRow += (s, e) =>
            {
                e.Row.MouseLeftButtonUp -= handler;
            };
        }

        public static string GetMethodName(DataGrid dataGrid)
        {
            return (string)dataGrid.GetValue(MethodNameProperty);
        }

        public static void SetMethodName(DataGrid dataGrid, string value)
        {
            dataGrid.SetValue(MethodNameProperty, value);
        }

        public static readonly DependencyProperty MethodNameProperty = DependencyProperty.RegisterAttached(
            "MethodName",
            typeof(string),
            typeof(RowClickHandler),
            new PropertyMetadata((o, e) =>
            {
                var dataGrid = o as DataGrid;
                if (dataGrid != null)
                {
                    new RowClickHandler(dataGrid);
                }
            }));

    }
}

WPF 代码


<DataGrid  AutoGenerateColumns="False" IsReadOnly="True" 
                   SelectedItem="{Binding SelectChainShop}"
                   helpers:RowClickHandler.MethodName="Edit"
                   CanUserDeleteRows="False" CanUserAddRows="False" GridLinesVisibility="None" Margin="25,184,266,31"
                   ItemsSource="{Binding chanShops}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="分组名" Width="100" Binding="{Binding name}" />
                <DataGridTextColumn Header="分组描述" Width="125" Binding="{Binding description}"/>
                <DataGridTextColumn Header="创建时间" Width="140" Binding="{Binding createOn}"/>
                <DataGridTextColumn Header="创建人" Width="100" Binding="{Binding createUserId}"/>
                <DataGridTextColumn Header="上次修改时间" Width="140"  Binding="{Binding modifiedOn}"/>
                <DataGridTextColumn Header="上次修改人" Width="90"  Binding="{Binding modifyUserId}"/>
            </DataGrid.Columns>
        </DataGrid>

MODULE代码实现

    public void RowClicked()
    {
        MessageBox.Show(SelectValue.name);
    }