一:先看看解决方案资源管理器截图:
二: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)); } } }