6、员工档案管理

1、基础框架准备工作
1.1UI层建立wpf应用程序EmployeeListUI.xaml与EmployeeListEdit.xaml;Model层建立Employee类, DAL层建立EmployeeDAL类。
1.2 创建数据库表

USE [HRMSYSDB] GO/****** Object:Table [dbo].[T_Employee]Script Date: 05/15/2013 11:22:50 ******/ SET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER ON GOCREATE TABLE [dbo].[T_Employee]( [Id] [uniqueidentifier] NOT NULL, [Number] [nvarchar](50) NOT NULL, [Name] [nvarchar](50) NOT NULL, [BirthDay] [datetime] NOT NULL, [InDate] [datetime] NOT NULL, [MarriageId] [uniqueidentifier] NOT NULL, [PartyStatusId] [uniqueidentifier] NOT NULL, [Nationality] [nvarchar](50) NOT NULL, [NativeAddr] [nvarchar](250) NOT NULL, [EducationId] [uniqueidentifier] NOT NULL, [Major] [nvarchar](50) NULL, [School] [nvarchar](50) NULL, [Address] [nvarchar](250) NOT NULL, [BaseSalary] [int] NOT NULL, [Email] [nvarchar](50) NULL, [IdNum] [nvarchar](50) NOT NULL, [TelNum] [nvarchar](50) NOT NULL, [EmergencyContact] [nvarchar](max) NULL, [DepartmentId] [uniqueidentifier] NOT NULL, [Position] [nvarchar](50) NOT NULL, [ContractStartDay] [datetime] NOT NULL, [ContractEndDay] [datetime] NOT NULL, [Resume] [nvarchar](max) NULL, [Remarks] [nvarchar](max) NULL, [IsStopped] [bit] NOT NULL, [GenderId] [uniqueidentifier] NOT NULL, [Photo] [image] NULL, CONSTRAINT [PK_T_Employee] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX= OFF, STATISTICS_NORECOMPUTE= OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS= ON, ALLOW_PAGE_LOCKS= ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'工号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'Number' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'入职时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'InDate' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'婚姻状况' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'MarriageId' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'政治面貌' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'PartyStatusId' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'民族' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'Nationality' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'籍贯' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'NativeAddr' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'学历' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'EducationId' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'专业' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'Major' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'毕业院校' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'School' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'联系地址' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'Address' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'基本工资' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'BaseSalary' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'身份证号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'IdNum' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'电话号码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'TelNum' GOEXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=https://www.it610.com/article/N'紧急联系人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'T_Employee', @level2type=N'COLUMN',@level2name=N'EmergencyContact' GO



2、Model层建立Employee类
与数据库表的字段对应,包括名称、类型,如数据字段为空者需要在类型后加?,因需要跨项目需要加public。
namespace HRMSys.Model { public class T_Employee { public System.Guid Id { get; set; } public System.String Number { get; set; } public System.String Name { get; set; } public System.DateTime BirthDay { get; set; } public System.DateTime InDate { get; set; } public System.Guid MarriageId { get; set; } public System.Guid PartyStatusId { get; set; } public System.String Nationality { get; set; } public System.String NativeAddr { get; set; } public System.Guid EducationId { get; set; } public System.String? Major { get; set; } public System.String? School { get; set; } public System.String Address { get; set; } public System.Int32 BaseSalary { get; set; } public System.String? Email { get; set; } public System.String IdNum { get; set; } public System.String TelNum { get; set; } public System.String? EmergencyContact { get; set; } public System.Guid DepartmentId { get; set; } public System.String Position { get; set; } public System.DateTime ContractStartDay { get; set; } public System.DateTime ContractEndDay { get; set; } public System.String? Resume { get; set; } public System.String? Remarks { get; set; } public System.Boolean IsStopped { get; set; } public System.Guid GenderId { get; set; } public System.Byte[] Photo { get; set; } }}



3、DAL层建立EmployeeDAL类
namespace HRMSys.DAL { public class EmployeeDAL { private Employee ToEmployee(DataRow row) { Employee employee = new Employee(); employee.Address = (string)row["Address"]; employee.BaseSalary = (int)row["BaseSalary"]; employee.BirthDay = (DateTime)row["BirthDay"]; employee.ContractEndDay = (DateTime)row["ContractEndDay"]; employee.ContractStartDay = (DateTime)row["ContractStartDay"]; employee.DepartmentId = (Guid)row["DepartmentId"]; employee.EducationId = (Guid)row["EducationId"]; employee.Email = (string)row["Email"]; employee.EmergencyContact = (string)SqlHelper.FromDbValue(row["EmergencyContact"]); employee.GenderId = (Guid)row["GenderId"]; employee.Id = (Guid)row["Id"]; employee.IdNum = (string)row["IdNum"]; employee.InDate = (DateTime)row["InDate"]; employee.Major = (string)row["Major"]; employee.MarriageId = (Guid)row["MarriageId"]; employee.Name = (string)row["Name"]; employee.Nationality = (string)row["Nationality"]; employee.NativeAddr = (string)row["NativeAddr"]; employee.Number = (string)row["Number"]; employee.PartyStatusId = (Guid)row["PartyStatusId"]; employee.Position = (string)row["Position"]; employee.Remarks = (string)SqlHelper.FromDbValue(row["Remarks"]); employee.Resume = (string)SqlHelper.FromDbValue(row["Resume"]); employee.School = (string)SqlHelper.FromDbValue(row["School"]); employee.TelNum = (string)row["TelNum"]; //todo:如果员工非常多,那么Photo会增加内存占用 employee.Photo = (byte[])SqlHelper.FromDbValue(row["Photo"]); return employee; } public Employee[] ListAll() { DataTable dt = SqlHelper.ExecuteDataTable("SELECT *FROM T_Employee where IsStopped = 0"); Employee[] empl = new Employee[dt.Rows.Count]; for (int i = 0; i < dt.Rows.Count; i++) { empl[i] = ToEmployee(dt.Rows[i]); //DataRow row = dt.Rows[i]; //Employee emp = new Employee(); //emp.Id =(Guid) row["Id"]; //empl[i] = emp; } return empl; } public void Insert(Employee employee) { SqlHelper.ExecuteNonQuery(@"INSERT INTO [T_Employee] ([Id],[Number],[Name],[BirthDay],[InDate],[MarriageId],[PartyStatusId],[Nationality] ,[NativeAddr],[EducationId],[Major],[School],[Address],[BaseSalary],[Email] ,[IdNum],[TelNum],[EmergencyContact],[DepartmentId],[Position],[ContractStartDay] ,[ContractEndDay],[Resume],[Remarks],[IsStopped],[GenderId],[Photo]) VALUES(newid(),@Number,@Name,@BirthDay,@InDate,@MarriageId,@PartyStatusId,@Nationality ,@NativeAddr,@EducationId,@Major,@School,@Address,@BaseSalary,@Email ,@IdNum,@TelNum,@EmergencyContact,@DepartmentId,@Position,@ContractStartDay ,@ContractEndDay,@Resume,@Remarks,0,@GenderId,@Photo)", new SqlParameter("@Number", employee.Number) , new SqlParameter("@Name", employee.Name) , new SqlParameter("@BirthDay", employee.BirthDay) , new SqlParameter("@InDate", employee.InDate) , new SqlParameter("@MarriageId", employee.MarriageId) , new SqlParameter("@PartyStatusId", employee.PartyStatusId) , new SqlParameter("@Nationality", employee.Nationality) , new SqlParameter("@NativeAddr", employee.NativeAddr) , new SqlParameter("@EducationId", employee.EducationId) , new SqlParameter("@Major", SqlHelper.ToDbValue(employee.Major)) , new SqlParameter("@School", SqlHelper.ToDbValue(employee.School)) , new SqlParameter("@Address", employee.Address) , new SqlParameter("@BaseSalary", employee.BaseSalary) , new SqlParameter("@Email", SqlHelper.ToDbValue(employee.Email)) , new SqlParameter("@IdNum", employee.IdNum) , new SqlParameter("@TelNum", employee.TelNum) , new SqlParameter("@EmergencyContact", SqlHelper.ToDbValue(employee.EmergencyContact)) , new SqlParameter("@DepartmentId", employee.DepartmentId) , new SqlParameter("@Position", employee.Position) , new SqlParameter("@ContractStartDay", employee.ContractStartDay) , new SqlParameter("@ContractEndDay", employee.ContractEndDay) , new SqlParameter("@Resume", SqlHelper.ToDbValue(employee.Resume)) , new SqlParameter("@Remarks", SqlHelper.ToDbValue(employee.Remarks)) , new SqlParameter("@GenderId", employee.GenderId) , new SqlParameter("@Photo",SqlHelper.ToDbValue(employee.Photo))); } /// /// 软删除 /// /// public void Deletet(Guid Id) { SqlHelper.ExecuteNonQuery("update T_Employee set IsStopped = 1 where Id = @Id", new SqlParameter("@Id",Id)); }/// /// GetByid,根据ID取得值,如果为空者取null,如果多个ID,者报错 /// /// /// public Employee GetById(Guid id) { DataTable dt =SqlHelper.ExecuteDataTable("SELECT *FROM T_Employee where IsStopped = 0 andId=@Id", new SqlParameter("@Id",id)); if (dt.Rows.Count < 0) { return null; } else if (dt.Rows.Count == 1) { return ToEmployee(dt.Rows[0]); } else { throw new Exception(); } } /// ///Update /// /// public void Update(Employee employee) { SqlHelper.ExecuteNonQuery(@"Update T_Employee set [Number]=@Number,[Name]=@Name,[BirthDay]=@BirthDay,[InDate]=@InDate, [MarriageId]=@MarriageId,[PartyStatusId]=@PartyStatusId,[Nationality]=@Nationality, [NativeAddr]=@NativeAddr,[EducationId]=@EducationId,[Major]=@Major,[School]=@School, [Address]=@Address,[BaseSalary]=@BaseSalary,[Email]=@Email, [IdNum]=@IdNum,[TelNum]=@TelNum,[EmergencyContact]=@EmergencyContact, [DepartmentId]=@DepartmentId,[Position]=@Position,[ContractStartDay]=@ContractStartDay, [ContractEndDay]=@ContractEndDay,[Resume]=@Resume,[Remarks]=@Remarks,[GenderId]=@GenderId,[Photo] = @Photo Where Id=@Id", new SqlParameter("@Number", employee.Number) , new SqlParameter("@Name", employee.Name) , new SqlParameter("@BirthDay", employee.BirthDay) , new SqlParameter("@InDate", employee.InDate) , new SqlParameter("@MarriageId", employee.MarriageId) , new SqlParameter("@PartyStatusId", employee.PartyStatusId) , new SqlParameter("@Nationality", employee.Nationality) , new SqlParameter("@NativeAddr", employee.NativeAddr) , new SqlParameter("@EducationId", employee.EducationId) , new SqlParameter("@Major", SqlHelper.ToDbValue(employee.Major)) , new SqlParameter("@School", SqlHelper.ToDbValue(employee.School)) , new SqlParameter("@Address", employee.Address) , new SqlParameter("@BaseSalary", employee.BaseSalary) , new SqlParameter("@Email", SqlHelper.ToDbValue(employee.Email)) , new SqlParameter("@IdNum", employee.IdNum) , new SqlParameter("@TelNum", employee.TelNum) , new SqlParameter("@EmergencyContact", SqlHelper.ToDbValue(employee.EmergencyContact)) , new SqlParameter("@DepartmentId", employee.DepartmentId) , new SqlParameter("@Position", employee.Position) , new SqlParameter("@ContractStartDay", employee.ContractStartDay) , new SqlParameter("@ContractEndDay", employee.ContractEndDay) , new SqlParameter("@Resume", SqlHelper.ToDbValue(employee.Resume)) , new SqlParameter("@Remarks", SqlHelper.ToDbValue(employee.Remarks)) , new SqlParameter("@GenderId", employee.GenderId) , new SqlParameter("@Id", employee.Id) , new SqlParameter("@Photo",employee.Photo)); } public Employee[] Search(string sql,List parameter) { DataTable dt =SqlHelper.ExecuteDataTable(sql,parameter.ToArray()); Employee[] empl = new Employee[dt.Rows.Count]; for (int i = 0; i < dt.Rows.Count; i++) { empl[i] = ToEmployee(dt.Rows[i]); } return empl; } //根据部门查询部门下的所有员工 /// /// 查询部门下的所有员工 /// /// /// public Employee[] ListByDepmentId(Guid deptiId) { DataTable dt = SqlHelper.ExecuteDataTable("SELECT *FROM T_Employee whereDepartmentId=@Id", new SqlParameter("@Id", deptiId)); return ToEmployees(dt); //可以凌下述内容封装一个方法 // Employee[] empl = new Employee[dt.Rows.Count]; //for (int i = 0; i < dt.Rows.Count; i++) //{ //empl[i] = ToEmployee(dt.Rows[i]); //} //return empl } private Employee[] ToEmployees(DataTable table) { Employee[] items = new Employee[table.Rows.Count]; for (int i = 0; i < table.Rows.Count; i++) { items[i] = ToEmployee(table.Rows[i]); } return items; }} }

【6、员工档案管理】

4、EmployeeListUI界面与交换
4.1 UI


4.2 交互
UI层第一次需要引用Model与DAL
namespace HRMSys.UI.SystemMgr { /// /// EmployeeListUI.xaml 的交互逻辑 /// public partial class EmployeeListUI : Window { public EmployeeListUI() { InitializeComponent(); }private void btnAdd_Click(object sender, RoutedEventArgs e) { //新增按钮 EmployeeEditUI edit = new EmployeeEditUI(); edit.IsAddNew = true; if (edit.ShowDialog() == true) { LoadData(); } }private void Window_Loaded_1(object sender, RoutedEventArgs e) { //column中的部门与学历 columnDepartmentId.ItemsSource = new DepartmentDAL().ListAll(); columnEducationId.ItemsSource = new IdNameDAL().GetByCategory("学历"); LoadData(); cbSearchByName.IsChecked = true; cbSearchByDept.IsChecked = true; cbSearchByBaseSalary.IsChecked = true; dpInDateStart.SelectedDate = DateTime.Today.AddMonths(-1); dpInDateEnd.SelectedDate = DateTime.Today; cmbDept.ItemsSource = new DepartmentDAL().ListAll(); } private void LoadData() { gridEmployee.ItemsSource = new EmployeeDAL().ListAll(); }private void bthDelete_Click(object sender, RoutedEventArgs e) { Employee empl = (Employee)gridEmployee.SelectedItem; if (empl == null) { MessageBox.Show("没有选中任何数据"); return; } else if (MessageBox.Show("你真的要删除吗", "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { new EmployeeDAL().Deletet(empl.Id); LoadData(); } }private void btnEdit_Click(object sender, RoutedEventArgs e) { Employee empl = (Employee)gridEmployee.SelectedItem; if (empl == null) { MessageBox.Show("你没有选择任何行!"); return; } EmployeeEditUI edit = new EmployeeEditUI(); edit.EditingId = empl.Id; edit.IsAddNew = false; if (edit.ShowDialog() == true) { LoadData(); }}private void btnSearch_Click(object sender, RoutedEventArgs e) {//搜索 List whereList = new List(); List parameter = new List(); if (cbSearchByName.IsChecked == true) { whereList.Add("Name=@Name"); parameter.Add(new SqlParameter("@Name",txtName.Text)); } if (cbSearchByInDate.IsChecked == true) { whereList.Add("InDate>=@InDateStart and InDate<=@InDateEnd"); parameter.Add(new SqlParameter("@InDateStart", dpInDateStart.SelectedDate)); parameter.Add(new SqlParameter("@InDateEnd", dpInDateEnd.SelectedDate)); } if (cbSearchByDept.IsChecked == true) { whereList.Add("DepartmentId=@DepartmentId"); parameter.Add(new SqlParameter("@DepartmentId", cmbDept.SelectedValue)); } if (cbSearchByBaseSalary.IsChecked == true) { whereList.Add("BaseSalary>=@BaseSalaryStart and BaseSalary<@BaseSalaryEnd"); parameter.Add(new SqlParameter("@BaseSalaryStart", cbBaseSalarStart.Text)); parameter.Add(new SqlParameter("@BaseSalaryEnd", cbBaseSalarEnd.Text)); } string whereSql = string.Join(" and ",whereList); string sql = "select * from T_Employee"; if (whereSql.Length > 0) { sql = sql + " where " + whereSql; } Employee[] result = new EmployeeDAL().Search(sql, parameter); gridEmployee.ItemsSource = result; }private void btExport_Click(object sender, RoutedEventArgs e) { SaveFileDialog sdfExport = new SaveFileDialog(); sdfExport.Filter = "Excel文件|*.xls"; if (sdfExport.ShowDialog() != true) { return; } string filename = sdfExport.FileName; HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("员工数据"); IRow rowHeader = sheet.CreateRow(0); //表头行 rowHeader.CreateCell(0, CellType.STRING).SetCellValue("姓名"); rowHeader.CreateCell(1, CellType.STRING).SetCellValue("工号"); rowHeader.CreateCell(2, CellType.STRING).SetCellValue("入职日期"); //把查询结果导出到Excel Employee[] employees = (Employee[])gridEmployee.ItemsSource; for (int i = 0; i < employees.Length; i++) { Employee employee = employees[i]; IRow row = sheet.CreateRow(i + 1); row.CreateCell(0, CellType.STRING).SetCellValue(employee.Name); row.CreateCell(1, CellType.STRING).SetCellValue(employee.Number); ICellStyle styledate = workbook.CreateCellStyle(); IDataFormat format = workbook.CreateDataFormat(); //格式具体有哪些请看单元格右键中的格式,有说明 styledate.DataFormat = format.GetFormat("yyyy\"年\"m\"月\"d\"日\""); ICell cellInDate = row.CreateCell(2, CellType.NUMERIC); cellInDate.CellStyle = styledate; cellInDate.SetCellValue(employee.InDate); }using (Stream stream = File.OpenWrite(filename)) { workbook.Write(stream); } } } }



5、EmployeeEditiUI UI界面与交互
5.1 UI


5.2 Edit交互

namespace HRMSys.UI.SystemMgr { /// /// EmployeeEditUI.xaml 的交互逻辑 /// public partial class EmployeeEditUI : Window { public EmployeeEditUI() { InitializeComponent(); } public Guid EditingId { get; set; } public bool IsAddNew {get; set; }private void Window_Loaded_2(object sender, RoutedEventArgs e) { IdNameDAL idNameDAL = new IdNameDAL(); cbGender.ItemsSource = idNameDAL.GetByCategory("性别"); cbGender.ItemsSource = idNameDAL.GetByCategory("性别"); cbMarriage.ItemsSource = idNameDAL.GetByCategory("婚姻状况"); cbPartyStatus.ItemsSource = idNameDAL.GetByCategory("政治面貌"); cbEducation.ItemsSource = idNameDAL.GetByCategory("学历"); cbDepatment.ItemsSource = new DepartmentDAL().ListAll(); if (IsAddNew) { //插入数据带入初始值 Employee employee = new Employee(); employee.InDate = DateTime.Today; //给默认值 employee.ContractStartDay = DateTime.Today; employee.ContractEndDay = DateTime.Today.AddYears(1); employee.BirthDay = DateTime.Today.AddYears(-30); employee.Nationality = "汉族"; employee.Email = "@szhnd.com.cn"; //手动指定 //employee.Number = "HND"; //系统设定 employee.Number = new T_SettingDAL().GetValue("员工工号前缀"); gridEmployee.DataContext = employee; } else { //这里是更新的初始值 Employee empl = new EmployeeDAL().GetById(EditingId); gridEmployee.DataContext = empl; if (empl.Photo != null) { ShowImg(empl.Photo); } //流操作,将图片显示出来 //建立一个empl.photo所指向的内存流。 //if (empl.Photo != null) //{ ////传入一个Photo数值,取得一个实例 //MemoryStream stream = new MemoryStream(empl.Photo); //BitmapImage image = new BitmapImage(); //image.StreamSource = stream; //imgPhoto.Source = image; //} }}private void txtSave_Click(object sender, RoutedEventArgs e) { bool isOK = true; CheckTextboxNotEmpty(ref isOK, txtName, txtNational, txtNativeAddr, txtAddr, txtBaseSalary, txtTelNum, txtIdNum, txtPosition, txtNumber); CheckComboBoxNotEmpty(ref isOK, cbGender, cbMarriage, cbPartyStatus, cbEducation, cbDepatment); //保存按钮 if (!isOK) { return; }if (IsAddNew) { //这里是新增 Employee employee = (Employee)gridEmployee.DataContext; new EmployeeDAL().Insert(employee); } else { //这里是编辑 Employee empl =(Employee) gridEmployee.DataContext; new EmployeeDAL().Update(empl); } DialogResult = true; } private void CheckTextboxNotEmpty(ref bool isOK, params TextBox[] textboxes) { foreach (TextBox txtBox in textboxes) { if (txtBox.Text.Length <= 0) { isOK = false; txtBox.Background = Brushes.Red; } else { txtBox.Background = null; } } } private void CheckComboBoxNotEmpty(ref bool isOK, params ComboBox[] cmbs) { foreach (ComboBox cmb in cmbs) { if (cmb.SelectedIndex < 0) { isOK = false; cmb.Effect = new DropShadowEffect { Color = Colors.Red }; } else { cmb.Effect = null; } } }private void btnChoosePhoto_Click(object sender, RoutedEventArgs e) { //选择照片 OpenFileDialog ofdPhoto = new OpenFileDialog(); ofdPhoto.Filter = "jpg图片|*.jpg|png图片|*.png"; if (ofdPhoto.ShowDialog() == true) { string filename = ofdPhoto.FileName; Employee employee = (Employee)gridEmployee.DataContext; employee.Photo = File.ReadAllBytes(filename); //读取文件的二进制数据 imgPhoto.Source = new BitmapImage(new Uri(filename)); } } private void ShowImg(byte[] imgBytes) { MemoryStream stream = new MemoryStream(imgBytes); BitmapImage bmpImg = new BitmapImage(); bmpImg.BeginInit(); bmpImg.StreamSource = stream; bmpImg.EndInit(); imgPhoto.Source = bmpImg; }private void btnCapture_Click(object sender, RoutedEventArgs e) { CaptureWindow win = new CaptureWindow(); if (win.ShowDialog() == true) { byte[] data = https://www.it610.com/article/win.CaptureData; ShowImg(data); Employee employee = (Employee)gridEmployee.DataContext; employee.Photo = data; } }}}


转载于:https://www.cnblogs.com/xjgby/archive/2013/05/15/3079525.html

    推荐阅读