设计数据库,表名T_Setting
插入数据如下:
此没有Model,只有DAL
新建SettingDAL类,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace HRMSys.DAL { public class SettingDAL { public void SetValue(string name, string value) { int i = SqlHelper.ExecuteNonQuery("Update T_Setting set Value=@Value where Name=@Name", new SqlParameter("@Value",value), new SqlParameter("@Name", name)); if (i != 1)//只可能出现在开发、测试阶段 { throw new Exception("影响行数不是1,而是"+i); } } public void SetValue(string name, bool value) { SetValue(name, value.ToString()); } public void SetValue(string name, int value) { SetValue(name, value.ToString()); } public string GetValue(string name) { DataTable table = SqlHelper.ExecuteDataTable("select Value from T_Setting where Name=@Name", new SqlParameter("@Name", name)); if (table.Rows.Count <= 0) { throw new Exception(name + "不存在!"); } else if (table.Rows.Count > 1) { throw new Exception("出现" + table.Rows.Count + "条Name=" + name + "的Settings数据"); } else { DataRow row = table.Rows[0]; return (string)row["Value"]; } } //todo:重载! public bool GetBoolValue(string name) { return Convert.ToBoolean(GetValue(name)); } public int GetIntValue(string name) { return Convert.ToInt32(GetValue(name)); } } }
在SystemMgr文件夹中新建一个窗体-SettingWindow.xaml,设计代码如下:
<Window x:Class="HRMSys.UI.SystemMgr.SettingWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="SettingWindow" Height="300" Width="300" Loaded="Window_Loaded"> <Grid> <TextBlock Height="23" HorizontalAlignment="Left" Margin="8,10,0,0" Name="textBlock1" Text="公司名称" VerticalAlignment="Top" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="62,0,0,0" Name="txtCompanyName" VerticalAlignment="Top" Width="145" /> <CheckBox Content="启用生日提醒,提前天数" Height="16" HorizontalAlignment="Left" Margin="8,77,0,0" Name="cbBirthDayPrompt" VerticalAlignment="Top" /> <TextBlock Height="23" HorizontalAlignment="Left" Margin="8,39,0,0" Name="textBlock2" Text="公司网站" VerticalAlignment="Top" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="62,38,0,0" Name="txtCompanySite" VerticalAlignment="Top" Width="145" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="165,70,0,0" Name="txtBirthDayDays" VerticalAlignment="Top" Width="42" /> <TextBlock Height="23" HorizontalAlignment="Left" Margin="11,105,0,0" Name="textBlock3" Text="工号前缀" VerticalAlignment="Top" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="62,104,0,0" Name="txtEmployeeNumberPrefix" VerticalAlignment="Top" Width="145" /> <Button Content="保存" Height="23" HorizontalAlignment="Left" Margin="153,179,0,0" Name="btnSave" VerticalAlignment="Top" Width="75" Click="btnSave_Click" /> </Grid> </Window>
窗体加载时候和保存按钮的事件如下;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using HRMSys.DAL; namespace HRMSys.UI.SystemMgr { /// <summary> /// SettingWindow.xaml 的交互逻辑 /// </summary> public partial class SettingWindow : Window { public SettingWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { SettingDAL dal = new SettingDAL(); txtCompanyName.Text = dal.GetValue("公司名称"); txtCompanySite.Text = dal.GetValue("公司网站"); cbBirthDayPrompt.IsChecked = dal.GetBoolValue("启用生日提醒"); txtBirthDayDays.Text = dal.GetValue("生日提醒天数"); txtEmployeeNumberPrefix.Text = dal.GetValue("员工工号前缀"); } private void btnSave_Click(object sender, RoutedEventArgs e) { SettingDAL dal = new SettingDAL(); dal.SetValue("公司名称", txtCompanyName.Text); dal.SetValue("公司网站", txtCompanySite.Text); dal.SetValue("启用生日提醒", (bool)cbBirthDayPrompt.IsChecked); dal.SetValue("生日提醒天数", txtBirthDayDays.Text); dal.SetValue("员工工号前缀", txtEmployeeNumberPrefix.Text); DialogResult = true; } } }
在主窗体MainWindow.xaml窗体中添加一个
<MenuItem Name="miSetting" Header="系统设置" Click="miSetting_Click"></MenuItem>
Click代码如下:
private void miSetting_Click(object sender, RoutedEventArgs e) { SettingWindow win = new SettingWindow(); win.ShowDialog(); }
其中要修改的部分是:
1.在MainWindow.xaml加载时候设置标题为公司名称,所以在他的Loaded事件中添加下列代码:
this.Title = new SettingDAL().GetValue("公司名称")+"人事管理系统";
2.在员工生日提醒的代码中修改成为如下:就是将最外层的if语句中的代码写在if语句里。
if (new SettingDAL().GetBoolValue("启用生日提醒")) { //检测一个月之内合同到期的员工 if (birthdayEmployees.Length > 0) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < birthdayEmployees.Length; i++) { sb.Append(birthdayEmployees[0].Name).Append(","); } sb.Append("三天之内过生日"); //MessageBox.Show(sb.ToString()); PopupWindow popupWin = new PopupWindow(); popupWin.Left = SystemParameters.WorkArea.Width - popupWin.Width; popupWin.Top = SystemParameters.WorkArea.Height - popupWin.Height; popupWin.Message = sb.ToString(); popupWin.Show(); } } }
3.在新增员工时候即在EmployeeEditWindow窗体加载的时候,判断是新增的时候,从数据库中读出员工编号前缀
//employee.Number = "YG"; employee.Number = new SettingDAL().GetValue("员工工号前缀");
最后就是试运行。
在运行时候需要补充的是,PopupWindow窗体,用于生日信息提醒,设计代码如下:
<Window x:Class="HRMSys.UI.PopupWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="提醒" Height="200" Width="300" ResizeMode="NoResize" Loaded="Window_Loaded"> <Grid> <TextBlock TextWrapping="Wrap" Name="txtMsg"></TextBlock> </Grid> </Window>
加载时候的事件代码如下:
public string Message { get; set; } private void Window_Loaded(object sender, RoutedEventArgs e) { txtMsg.Text = Message; }
public string Message { get; set; }
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txtMsg.Text = Message;
}