日期:2014-06-10 浏览次数:20657 次
一:先看看解决方案资源管理器截图:
二:DepartmentDAL.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 DepartmentDAL { /// <summary> /// 将得到的表的形式转化为object对象字段的形式 /// </summary> /// <param name="row"></param> /// <returns></returns> private Department ToModel(DataRow row) { Department dept = new Department(); dept.Id = (Guid)row["Id"]; dept.Name = (string)row["Name"]; dept.IsDelete=(bool) row["IsDelete"]; return dept; } /// <summary> /// 显示所有未删除的部门信息 /// </summary> /// <returns>以部门为对象的泛型集合</returns> public IEnumerable<Department> ListAll() //public List<Department> ListAll() { List<Department> list = new List<Department>(); DataTable dt = sqlhelper.datatable("select * from T_Department where IsDelete=0"); foreach (DataRow row in dt.Rows) { Department dept = ToModel(row); list.Add(dept); } return list; } /// <summary> /// 得到指定id的部门 /// </summary> /// <param name="id"></param> /// <returns>部门对象</returns> public Department GetById(Guid id) { DataTable dt = sqlhelper.datatable("select * from T_Department where Id=@Id", new SqlParameter("@Id", id)); if (dt.Rows.Count <= 0) { return null; } else { return ToModel(dt.Rows[0]); } } /// <summary> /// 更新指定部门id的部门名字 /// </summary> /// <param name="id"></param> /// <param name="name"></param> public void Update(Guid id, string name) { sqlhelper.ExecuteNon(@"Update T_Department Set Name=@Name where Id=@Id", new SqlParameter("@Name", name), new SqlParameter("@Id", id)); } /// <summary> /// 插入一条部门信息 /// </summary> /// <param name="name"></param> public void Insert(string name) { sqlhelper.ExecuteNon(@"Insert Into T_Department(Id,Name,IsDelete) values(newid(),@Name,0)", new SqlParameter("@Name", name)); } /// <summary> /// 软删除指定部门id的部门 /// </summary> /// <param name="id"></param> public void DeleteById(Guid id) { sqlhelper.ExecuteNon(@"Update T_Department Set IsDelete=1 where Id=@Id", new SqlParameter("@Id", id)); } } }
三: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]) 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)", 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) ); } /// <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 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)); } } }
四:IdNameDAL.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using HRMSys.Model; using System.Data.SqlClient; using System.Data; namespace HRMSys.DAL { public class IdNameDAL { //按分类得到所要的数据 public IdName[] GetByCategery(string categery) { DataTable table= sqlhelper.datatable("select Id,Name from T_IdName where Categery=@Categery", new SqlParameter("@Categery", categery)); IdName[] idnam = new IdName[table.Rows.Count] ; for (int i = 0; i < table.Rows.Count; i++) { DataRow row=table.Rows[i]; IdName idname = new IdName(); idname.Id=(Guid) row["Id"]; idname.Name=(string)row["Name"]; idnam[i] = idname; } return idnam; } } }
五:sqlhelper.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace HRMSys.DAL { static class sqlhelper { static SqlConnection conn; static SqlCommand cmd; /// <summary> /// 返回受影响的行数 /// </summary> public static readonly string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; public static int ExecuteNon(string sql,params SqlParameter[] parameter ) { using (conn = new SqlConnection(constr)) { conn.Open(); using ( cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameter); return cmd.ExecuteNonQuery(); } } } /// <summary> ///返回结果的首行首列 /// </summary> /// <param name="sql"></param> /// <param name="parameter"></param> /// <returns></returns> public static int ExecuteSca(string sql, params SqlParameter[] parameter) { using (conn = new SqlConnection(constr)) { conn.Open(); using (cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameter); int result = (int) cmd.ExecuteScalar(); return result; } } } /// <summary> /// 返回一个数据流 /// </summary> /// <param name="sql"></param> /// <param name="parameter"></param> /// <returns></returns> public static object SqldataRea(string sql, params SqlParameter[] parameter) { using ( conn = new SqlConnection(constr)) { conn.Open(); using (cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameter); SqlDataReader sdr= sdr=cmd.ExecuteReader(); return sdr; } } } /// <summary> /// 返回一个表 /// </summary> /// <param name="sql"></param> /// <param name="parameter"></param> /// <returns></returns> public static DataTable datatable(string sql, params SqlParameter[] parameter) { using ( conn = new SqlConnection(constr)) { conn.Open(); using (cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameter); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); return ds.Tables[0]; } } } /// <summary> /// 可空的处理,将数据库的可空类型转换为vs里的可空类型 /// </summary> /// <param name="value"></param> /// <returns></returns> public static object FromDbValue(object value) { if (value == DBNull.Value) { return null; } else { return value; } } /// <summary> /// 将vs里的可空类型转换为数据库里的可空类型 /// </summary> /// <param name="value"></param> /// <returns></returns> public static object ToDbValue(object value) { if (value == null) { return DBNull.Value; } else { return value; } } } }
六:Department.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HRMSys.Model { public class Department { public Guid Id { get; set; } public string Name { get; set; } public bool IsDelete { 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; } } }
八: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; } } }
九:IdName.cs源代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HRMSys.Model { public class IdName { public Guid Id { get;set;} public string Name{get;set;} } }
十:DeptEditWindow.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; namespace HYMSys.UI.SystemMgr { public partial class DeptEditWindow : Form { public DeptEditWindow() { InitializeComponent(); } public bool IsAdd { get; set; } public Guid DepartmentId { get; set; } public bool load { get; set; } /// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { if (IsAdd == true) { DepartmentDAL dal = new DepartmentDAL(); dal.Insert(tb_Dename.Text); MessageBox.Show("插入成功"); } else { DepartmentDAL dal = new DepartmentDAL(); dal.Update(DepartmentId, tb_Dename.Text); MessageBox.Show("修改成功"); } } /// <summary> /// 取消 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { DeptEditWindow ed = new DeptEditWindow(); this.Close(); } /// <summary> /// 窗口关闭设置刷新list窗口的参数为true /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeptEditWindow_FormClosing(object sender, FormClosingEventArgs e) { load = true; } } }
十一:DeptMgrWindow.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; namespace HYMSys.UI.SystemMgr { public partial class DeptMgrWindow : Form { public DeptMgrWindow() { InitializeComponent(); } private void load() { DepartmentDAL de = new DepartmentDAL(); dataGridView1.DataSource = de.ListAll(); } /// <summary> /// 窗体自动加载部门信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeptMgrWindow_Load(object sender, EventArgs e) { load(); } /// <summary> /// 部门添加 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolsb_add_Click(object sender, EventArgs e) { DeptEditWindow ed = new DeptEditWindow(); ed.IsAdd = true; ed.ShowDialog(); if (ed.load == true) { //ed.load这个是刚新建的窗口的字段 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[1].Value; DepartmentDAL da = new DepartmentDAL(); //MessageBox.Show("真的要删除--"+name+"--吗?","警告!",MessageBoxButtons.OKCancel)==DialogResult.Yes)是Yes将始终不成立,不会删除 if (MessageBox.Show("真的要删除--"+name+"--吗?","警告!",MessageBoxButtons.OKCancel)==DialogResult.OK) { da.DeleteById(id); load(); MessageBox.Show(name+"已删除"); } } /// <summary> /// 部门修改 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolsb_edit_Click(object sender, EventArgs e) { DeptEditWindow ed = new DeptEditWindow(); ed.DepartmentId=(Guid)dataGridView1.CurrentRow.Cells[0].Value; ed.IsAdd = false; ed.ShowDialog(); if (ed.load == true) { load(); } } } }
十二: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; 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); //pb_Photo.DataBindings.Add(new Binding("pb_Photo", em, "Photo", true)); 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(); list = dal.GetById(EditId); em = list; 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("更改成功!"); } } } }
十三:EmployeeListWindow.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; 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[1].Value; string name=(string)dataGridView1.CurrentRow.Cells[3].Value; 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[1].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(); } } }
十四:作品图