员工照片加载
一:EmployeeList.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HRMSys.Model { public class EmployeeList { public Guid Id { get; set; } /// <summary> /// 工号 /// </summary> public string Number { get; set; } public string Name { get; set; } /// <summary> /// 性别 /// </summary> public string Gender { get; set; } //因为图片如果一次性读取到Model中会非常耗内存,因此需要的时候再去单独读取,不在Model中建字段 /// <summary> /// 出生日期 /// </summary> public DateTime BirthDay { get; set; } /// <summary> /// 入职日期 /// </summary> public DateTime InDate { get; set; } /// <summary> /// 婚姻状态 /// </summary> public string Marriage { get; set; } /// <summary> /// 政治面貌 /// </summary> public string PartyStatus { get; set; } /// <summary> /// 民族 /// </summary> public string Nationality { get; set; } /// <summary> /// 籍贯 /// </summary> public string NativeAddr { get; set; } /// <summary> /// 教育程度 /// </summary> public string Education { get; set; } /// <summary> /// 专业 /// </summary> public string Major { get; set; } /// <summary> /// 毕业院校 /// </summary> public string School { get; set; } /// <summary> /// 地址 /// </summary> public string Address { get; set; } /// <summary> /// 基本工资 /// </summary> public int BaseSalary { get; set; } /// <summary> /// Email /// </summary> public string Email { get; set; } /// <summary> /// 有效身份证号 /// </summary> public string IdNum { get; set; } /// <summary> /// 联系电话 /// </summary> public string TelNum { get; set; } /// <summary> /// 紧急联系人信息 /// </summary> public string EmergencyContact { get; set; } /// <summary> /// 部门 /// </summary> public string Department { get; set; } /// <summary> /// 职位 /// </summary> public string Position { get; set; } /// <summary> /// 合同起始时间 /// </summary> public DateTime ContractStartDay { get; set; } /// <summary> /// 合同到期时间 /// </summary> public DateTime ContractEndDay { get; set; } /// <summary> /// 简历 /// </summary> public string Resume { get; set; } /// <summary> /// 备注 /// </summary> public string Remarks { get; set; } /// <summary> /// 照片 /// </summary> public byte[] Photo { get; set; } public bool IsStopped { get; set; } } }
二:Employee.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HRMSys.Model { public class Employee { public Guid Id { get; set; } /// <summary> /// 工号 /// </summary> public string Number { get; set; } public string Name { get; set; } /// <summary> /// 性别Id /// </summary> public Guid GenderId { get; set; } //因为图片如果一次性读取到Model中会非常耗内存,因此需要的时候再去单独读取,不在Model中建字段 /// <summary> /// 出生日期 /// </summary> public DateTime BirthDay { get; set; } /// <summary> /// 入职日期 /// </summary> public DateTime InDate { get; set; } /// <summary> /// 婚姻状态Id /// </summary> public Guid MarriageId { get; set; } /// <summary> /// 政治面貌Id /// </summary> public Guid PartyStatusId { get; set; } /// <summary> /// 民族 /// </summary> public string Nationality { get; set; } /// <summary> /// 籍贯 /// </summary> public string NativeAddr { get; set; } /// <summary> /// 教育程度Id /// </summary> public Guid EducationId { get; set; } /// <summary> /// 专业 /// </summary> public string Major { get; set; } /// <summary> /// 毕业院校 /// </summary> public string School { get; set; } /// <summary> /// 地址 /// </summary> public string Address { get; set; } /// <summary> /// 基本工资 /// </summary> public int BaseSalary { get; set; } /// <summary> /// Email /// </summary> public string Email { get; set; } /// <summary> /// 有效身份证号 /// </summary> public string IdNum { get; set; } /// <summary> /// 联系电话 /// </summary> public string TelNum { get; set; } /// <summary> /// 紧急联系人信息 /// </summary> public string EmergencyContact { get; set; } /// <summary> /// 部门Id /// </summary> public Guid DepartmentId { get; set; } /// <summary> /// 职位 /// </summary> public string Position { get; set; } /// <summary> /// 合同起始时间 /// </summary> public DateTime ContractStartDay { get; set; } /// <summary> /// 合同到期时间 /// </summary> public DateTime ContractEndDay { get; set; } /// <summary> /// 简历 /// </summary> public string Resume { get; set; } /// <summary> /// 备注 /// </summary> public string Remarks { get; set; } /// <summary> /// 照片 /// </summary> public byte[] Photo { get; set; } public bool IsStopped { get; set; } } }
三:EmployeeDAL.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using HRMSys.Model; using System.Data; using System.Data.SqlClient; namespace HRMSys.DAL { public class EmployeeDAL { /// <summary> /// 将表的列转换为EmployeeList对象的字段 /// </summary> /// <param name="row"></param> /// <returns></returns> public EmployeeList ToEmployeeListModel(DataRow row) { EmployeeList employee = new EmployeeList(); 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.Department =GetNameByGuidDE( (Guid)row["DepartmentId"]);////// employee.Education= GetNameByGuidID((Guid)row["EducationId"]);///// employee.Email = (string)row["Email"]; employee.EmergencyContact = (string)sqlhelper.FromDbValue(row["EmergencyContact"]); employee.Gender = GetNameByGuidID((Guid)row["GenderId"]);/////// employee.Id = (Guid)row["Id"]; employee.IdNum = (string)row["IdNum"]; employee.InDate = (DateTime)row["InDate"]; employee.Major = (string)sqlhelper.FromDbValue(row["Major"]); employee.Marriage = GetNameByGuidID((Guid)row["MarriageId"]);///////// employee.Name = (string)row["Name"]; employee.Nationality = (string)row["Nationality"]; employee.NativeAddr = (string)row["NativeAddr"]; employee.Number = (string)row["Number"]; employee.PartyStatus = GetNameByGuidID((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"]; employee.IsStopped = (bool)row["IsStopped"]; //todo:如果员工非常多,那么Photo会增加内存占用 employee.Photo = (byte[])sqlhelper.FromDbValue(row["Photo"]); return employee; } /// <summary> /// 将表转换为Employee对象的字段 /// </summary> /// <param name="row"></param> /// <returns></returns> public Employee ToEmployeeModel(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)sqlhelper.FromDbValue(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"]; employee.IsStopped = (bool)row["IsStopped"]; //todo:如果员工非常多,那么Photo会增加内存占用 employee.Photo = (byte[])sqlhelper.FromDbValue(row["Photo"]); return employee; } /// <summary> /// 软删除指定id的员工信息 /// </summary> /// <param name="id"></param> public void DeleteById(Guid id) { //update T_Operator set IsLocked=@IsLocked where Id=@Id" int i= sqlhelper.ExecuteNon("update T_Employee set IsStopped =1 where Id=@Id", new SqlParameter ("@Id",id)); } /// <summary> /// 指定id从idname中取得名字 /// </summary> /// <param name="id"></param> /// <returns></returns> public string GetNameByGuidID(Guid id) { DataTable table= sqlhelper.datatable("select Name from T_IdName where Id=@Id", new SqlParameter("@Id",id)); DataRow row = table.Rows[0]; return (string) row["Name"]; } /// <summary> /// 指定id从department中取得名字 /// </summary> /// <param name="id"></param> /// <returns></returns> public string GetNameByGuidDE(Guid id) { DataTable table = sqlhelper.datatable("select Name from T_Department where Id=@Id", new SqlParameter("@Id", id)); DataRow row = table.Rows[0]; return (string)row["Name"]; } /// <summary> /// 得到所有未删除的员工的所有信息 /// </summary> /// <returns>Employee数组</returns> public EmployeeList[] ListAll() { DataTable table = sqlhelper.datatable("select * from T_Employee where IsStopped=0"); EmployeeList[] items = new EmployeeList[table.Rows.Count]; for (int i = 0; i < table.Rows.Count; i++) { EmployeeList employee = ToEmployeeListModel(table.Rows[i]); items[i] = employee; } return items; } /// <summary> /// 得到指定id的员工信息 /// </summary> /// <param name="id"></param> /// <returns>Employee对象</returns> public Employee GetById(Guid id) { DataTable table = sqlhelper.datatable("select * from T_Employee where Id=@Id", new SqlParameter("@Id", id)); if (table.Rows.Count == 1) { return ToEmployeeModel(table.Rows[0]); } else { throw new Exception(); } } /// <summary> /// 插入一条员工数据 /// </summary> /// <param name="employee"></param> public void Insert(Employee employee) { sqlhelper.ExecuteNon(@"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", SqlDbType.Image) {Value= sqlhelper.ToDbValue(employee.Photo) }//需要指定专门image的类型,数据库不认识, ); } /// <summary> /// 更新一条员工数据数据 /// </summary> /// <param name="employee"></param> public void Update(Employee employee) { sqlhelper.ExecuteNon(@"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("@Photo", SqlDbType.Image) {Value= sqlhelper.ToDbValue(employee.Photo) }//需要指定专门image的类型,数据库不认识, , new SqlParameter("@Id", employee.Id) ); } } }
四:EmployeeEditWindow.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using HRMSys.DAL; using HRMSys.Model; using System.IO; namespace HYMSys.UI.EmployeeMgr//不要将文件夹的名字写的和model的名字一样,这样就将命名空间和model重名了 { public partial class EmployeeEditWindow : Form { public EmployeeEditWindow() { InitializeComponent(); } public bool IsAdd { get; set; } public Guid EditId { get; set; } public bool IsLoad { get; set; } public bool IsSave { get; set; } Employee em = new Employee(); //得到有默认值的tb的text长度 int number; /// <summary> /// 绑定数据 /// </summary> public void binding() { DepartmentDAL dal = new DepartmentDAL(); IdNameDAL idName = new IdNameDAL(); cb_EducationId.DataSource = idName.GetByCategery("学历"); cb_GenderId.DataSource = idName.GetByCategery("性别"); cb_MarriageId.DataSource = idName.GetByCategery("婚姻状况"); cb_PartyStatusId.DataSource = idName.GetByCategery("政治面貌"); cb_DepartmentId.DataSource = dal.ListAll(); IsSave = true; //部门id cb_DepartmentId.DisplayMember = "Name"; cb_DepartmentId.ValueMember = "Id"; if (em.DepartmentId != null) cb_DepartmentId.SelectedValue = em.DepartmentId; //学历id cb_EducationId.DisplayMember = "Name"; cb_EducationId.ValueMember = "Id"; if (em.DepartmentId != null)//如果不为空将选择好的数据绑定上 cb_EducationId.SelectedValue = em.EducationId; //性别id cb_GenderId.DisplayMember = "Name"; cb_GenderId.ValueMember = "Id"; if (em.DepartmentId != null) cb_GenderId.SelectedValue = em.GenderId; //婚姻状况id cb_MarriageId.DisplayMember = "Name"; cb_MarriageId.ValueMember = "Id"; if (em.DepartmentId != null) cb_MarriageId.SelectedValue = em.MarriageId; //政治面貌id cb_PartyStatusId.DisplayMember = "Name"; cb_PartyStatusId.ValueMember = "Id"; if (em.DepartmentId != null) cb_PartyStatusId.SelectedValue = em.PartyStatusId; //其他的数据绑定 tb_Address.DataBindings.Add("Text", em, "Address", false, DataSourceUpdateMode.OnPropertyChanged); tb_BaseSalary.DataBindings.Add("Text", em, "BaseSalary", false, DataSourceUpdateMode.OnPropertyChanged); dtp_BirthDay.DataBindings.Add("Value", em, "BirthDay", true, DataSourceUpdateMode.OnPropertyChanged); dtp_ContractEndDay.DataBindings.Add("Value", em, "ContractEndDay", true, DataSourceUpdateMode.OnPropertyChanged); dtp_ContractStartDay.DataBindings.Add("Value", em, "ContractStartDay", true, DataSourceUpdateMode.OnPropertyChanged); //dtp_ContractStartDay.DataBindings.Add("Value", em, "ContractStartDay", true); tb_Email.DataBindings.Add("Text", em, "Email", false, DataSourceUpdateMode.OnPropertyChanged); tb_EmergencyContact.DataBindings.Add("Text", em, "EmergencyContact", false, DataSourceUpdateMode.OnPropertyChanged); tb_IdNum.DataBindings.Add("Text", em, "IdNum", false, DataSourceUpdateMode.OnPropertyChanged); dtp_Indate.DataBindings.Add("Value", em, "InDate", true, DataSourceUpdateMode.OnPropertyChanged); tb_Major.DataBindings.Add("Text", em, "Major", false, DataSourceUpdateMode.OnPropertyChanged); tb_Name.DataBindings.Add("Text", em, "Name", false, DataSourceUpdateMode.OnPropertyChanged); tb_Nationality.DataBindings.Add("Text", em, "Nationality", false, DataSourceUpdateMode.OnPropertyChanged); tb_NativeAddr.DataBindings.Add("Text", em, "NativeAddr", false, DataSourceUpdateMode.OnPropertyChanged); tb_Number.DataBindings.Add("Text", em, "Number", false, DataSourceUpdateMode.OnPropertyChanged); tb_Position.DataBindings.Add("Text", em, "Position", false, DataSourceUpdateMode.OnPropertyChanged); tb_Remarks.DataBindings.Add("Text", em, "Remarks", false, DataSourceUpdateMode.OnPropertyChanged); //tabControl1.TabPages[2].DataBindings.Add("Text", em, "Resume", true, DataSourceUpdateMode.OnPropertyChanged); tb_school.DataBindings.Add("Text", em, "School", false, DataSourceUpdateMode.OnPropertyChanged); tb_TelNum.DataBindings.Add("Text", em, "TelNum", false, DataSourceUpdateMode.OnPropertyChanged); } /// <summary> /// 窗口关闭时将刷新置为true /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EmployeeEditWindow_FormClosing(object sender, FormClosingEventArgs e) { IsLoad = true; } /// <summary> /// 添加时自动载入默认值,数据绑定和初始化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EmployeeEditWindow_Load(object sender, EventArgs e) { if (IsAdd==true) { #region //combobox的数据绑定 binding(); //将合同到期日在今天的基础上加1年 dtp_ContractEndDay.Value = DateTime.Now.AddYears(1); dtp_BirthDay.Value = DateTime.Now.AddYears(-24); dtp_ContractStartDay.Value = DateTime.Now; dtp_Indate.Value = DateTime.Now; tb_Nationality.Text = "汉族"; tb_BaseSalary.Text = "3000"; tb_Email.Text = "@qq.com"; tb_Number.Text = "HMJ"; number = tb_Number.Text.Length; #endregion } else { Employee list = new Employee(); EmployeeDAL dal = new EmployeeDAL(); em= dal.GetById(EditId); byte[] image = em.Photo; if (em.Photo != null) { MemoryStream ms = new MemoryStream(image); Bitmap bm = new Bitmap(ms); pb_Photo.Image = bm; em.Photo = image; } binding(); } } /// <summary> /// textboxToRed将背景颜色变红 /// </summary> /// <param name="tb"></param> private void textboxToRed(TextBox tb,int length) { if (tb.Text.Length <= length) { tb.BackColor = Color.Red;//将背景变为红色 IsSave = false; } else { tb.BackColor = DefaultBackColor;//将背景置为默认颜色 } } /// <summary> /// ComboxToRed将背景颜色变红 /// </summary> /// <param name="tb"></param> private void ComboxToRed(ComboBox cb) { if (cb.Text.Length <= 0) { cb.BackColor = Color.Red;//将背景变为红色 IsSave = false; } else { cb.BackColor = DefaultBackColor;//将背景置为默认颜色 } } /// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { if (IsAdd) { #region //检查必填项,如果为空显示为红色 textboxToRed(tb_Address, 0); textboxToRed(tb_IdNum, 0); textboxToRed(tb_Name, 0); textboxToRed(tb_Number, number);//工号和默认值长度比较 textboxToRed(tb_Position, 0); textboxToRed(tb_TelNum, 0); textboxToRed(tb_NativeAddr, 0); ComboxToRed(cb_DepartmentId); ComboxToRed(cb_EducationId); ComboxToRed(cb_GenderId); ComboxToRed(cb_MarriageId); ComboxToRed(cb_PartyStatusId); //这里不用再赋值,已经绑定了,怎么将外键id显示为对应的名字呢?cb_PartyStatusId.SelectedValue = em.PartyStatusId; #region //EmployeeDAL dal = new EmployeeDAL(); //Employee em = new Employee(); //em.EducationId = cb_EducationId.ValueMember;// //em.Address = tb_Address.Text; //em.BaseSalary = Convert.ToInt32(tb_BaseSalary.Text); //em.BirthDay = dtp_BirthDay.Value; //em.ContractEndDay = dtp_ContractEndDay.Value; //em.ContractStartDay = dtp_ContractStartDay.Value; //em.DepartmentId = cb_DepartmentId.ValueMember; //em.Email = tb_Email.Text; //em.EmergencyContact = tb_EmergencyContact.Text; //em.GenderId = cb_GenderId.ValueMember; ////em.Id=不用输的 //em.IdNum = tb_IdNum.Text; //em.InDate = dtp_Indate.Value; //em.Major = tb_Major.Text; //em.MarriageId = cb_MarriageId.ValueMember;//要转换为guid婚姻 //em.Name = tb_Name.Text; //em.Nationality = tb_Nationality.Text; //em.NativeAddr = tb_NativeAddr.Text; //em.Number = tb_Number.Text; //em.PartyStatusId = cb_PartyStatusId.ValueMember; //em.Photo = pb_Photo; //em.Position = tb_Position.Text; //em.Remarks = tb_Remarks.Text; //em.Resume = tabControl1.TabPages[2].ToString();//简历 //em.School = tb_school.Text; //em.TelNum = tb_TelNum.Text; #endregion if (IsSave == true) { EmployeeDAL darl = new EmployeeDAL(); //将数据结果给object对象 em.DepartmentId = (Guid)cb_DepartmentId.SelectedValue; em.EducationId = (Guid)cb_EducationId.SelectedValue; em.GenderId = (Guid)cb_GenderId.SelectedValue; em.MarriageId = (Guid)cb_MarriageId.SelectedValue; em.PartyStatusId = (Guid)cb_PartyStatusId.SelectedValue; darl.Insert(em); MessageBox.Show("成功插入一条员工数据"); IsLoad = true; } #endregion } else { EmployeeDAL dal = new EmployeeDAL(); dal.Update(em); MessageBox.Show("更改成功!"); } } /// <summary> /// 选择照片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { //读取图片路径,转换为二进制的数据流 openFileDialog1.Filter = "jpg图片|*.jpg|png图片|*.png"; string name = openFileDialog1.FileName; FileStream fs = new FileStream(name, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); byte[] image = br.ReadBytes((int)fs.Length); MemoryStream ms = new MemoryStream(image); Bitmap bm = new Bitmap(ms); pb_Photo.Image = bm; em.Photo = image; } } } }
五:EmployeeListWindow.cx
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using HRMSys.DAL; using HRMSys.Model; namespace HYMSys.UI.EmployeeMgr { public partial class EmployeeListWindow : Form { public EmployeeListWindow() { InitializeComponent(); } /// <summary> /// 加载所有信息 /// </summary> public EmployeeList[] load() { EmployeeDAL de = new EmployeeDAL(); return de.ListAll(); } /// <summary> /// 添加员工 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolsb_add_Click(object sender, EventArgs e) { EmployeeEditWindow edit = new EmployeeEditWindow(); edit.IsAdd = true; edit.ShowDialog(); if (edit.IsLoad == true) { dataGridView1.DataSource = load(); } } /// <summary> /// 删除员工 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolsb_delete_Click(object sender, EventArgs e) { Guid id=(Guid)dataGridView1.CurrentRow.Cells[0].Value; string name=(string)dataGridView1.CurrentRow.Cells[2].Value;//这里的cell是相对于list员工操作窗口上name这列,即使第二行 if (MessageBox.Show("真的要删除---"+name+"---吗?","警告!",MessageBoxButtons.OKCancel)==DialogResult.OK) { EmployeeDAL dal = new EmployeeDAL(); dal.DeleteById(id); dataGridView1.DataSource = load(); } } /// <summary> /// 修改员工信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolsb_edit_Click(object sender, EventArgs e) { EmployeeEditWindow edit = new EmployeeEditWindow(); edit.EditId = (Guid)dataGridView1.CurrentRow.Cells[0].Value; edit.IsAdd = false; edit.ShowDialog(); if (edit.IsLoad == true) { dataGridView1.DataSource = load(); } } /// <summary> /// 自动加载所有员工信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EmployeeListWindow_Load(object sender, EventArgs e) { dataGridView1.DataSource= load(); } } }