public ActionResult Index()
{
//ViewBag.EmpList = db.Employee;
//联合查询
string sql = "select * from Employee e join Dept d on e.DeptId = d.DeptId";
ViewBag.EmpList = db.Database.SqlQuery(typeof(EmployeeViewModel), sql);
ViewBag.DeptList = db.Dept;
return View();
[HttpPost]
public ActionResult Index(string EmpName,int DeptId)
{
string sql = "select * from Employee e join Dept d on e.DeptId = d.DeptId where 1=1 ";
sql += (EmpName != "") ? " and EmpName like @EmpName" : "";
sql += (DeptId != 0) ? " and e.DeptId=@DeptId" : "";
SqlParameter[] paras =
{
new SqlParameter("@EmpName","%"+EmpName+"%"),
new SqlParameter("@DeptId",DeptId)
};
ViewBag.EmpList = db.Database.SqlQuery(typeof(EmployeeViewModel), sql, paras);
ViewBag.EmpName = EmpName;
ViewBag.DeptId = DeptId;
ViewBag.DeptList = db.Dept;
return View();
}
Index页面添加员工姓名,部门名称及查询按钮:
员工信息列表
添加员工
//添加员工
public ActionResult AddEmp()
{
// Employee emp = new Employee()
// {
// EmpName = Request["EmpName"],
// DeptId = int.Parse(Request["DeptId"]),
// EmpPhone = Request["EmpPhone"],
// EmpArea = Request["EmpArea"],
// EmpSalary =decimal.Parse(Request["EmpSalary"])
// };
// db.Employee.Add(emp);
//int count = db.SaveChanges();
string sql = "insert into Employee(EmpName,DeptId,EmpPhone,EmpArea,EmpSalary)values (@EmpName,@DeptId,@EmpPhone,@EmpArea,@EmpSalary)";
SqlParameter[] ps =
{
new SqlParameter ("@EmpName", Request["EmpName"]),
new SqlParameter ("@DeptId",int.Parse(Request["DeptId"])),
new SqlParameter ("@EmpPhone",Request["EmpPhone"]),
new SqlParameter ("@EmpArea",Request["EmpArea"]),
new SqlParameter ("@EmpSalary",decimal.Parse(Request["EmpSalary"]))
};
int count = db.Database.ExecuteSqlCommand(sql,ps );
if (count>0)
{
return Content("");
}
else
{
return Content("");
}
}
//编辑员工页面
public ActionResult EditEmpForm()
{
ViewBag.DeptList = db.Dept;
//获取主键
ViewBag.Emp = db.Employee.Find(int.Parse(Request["EmpId"]));
return View();
}
//编辑员工
public ActionResult EditEmp()
{
//获取主键
int EmpId = int.Parse(Request["EmpId"]);
//1.EF框架执行修改操作
//var emp = db.Employee.Find(EmpId);
//emp.EmpName = Request["EmpName"];
//emp.DeptId = int.Parse(Request["DeptId"]);
//emp.EmpPhone = Request["EmpPhone"];
//emp.EmpArea = Request["EmpArea"];
//emp.EmpSalary = decimal.Parse(Request["EmpSalary"]);
//int count = db.SaveChanges();
//2.EF框架执行sql语句
string sql = "update Employee set DeptId=@DeptId,EmpName=@EmpName,EmpPhone=@EmpPhone,EmpArea=@EmpArea,EmpSalary=@EmpSalary where EmpId=@EmpId";
SqlParameter[] paras =
{
new SqlParameter("@DeptId", int.Parse(Request["DeptId"])),
new SqlParameter("@EmpName", Request["EmpName"]),
new SqlParameter("@EmpPhone", Request["EmpPhone"]),
new SqlParameter("@EmpArea", Request["EmpArea"]),
new SqlParameter("@EmpSalary", decimal.Parse(Request["EmpSalary"])),
new SqlParameter("@EmpId", EmpId)
};
int count = db.Database.ExecuteSqlCommand(sql, paras);
if (count > 0)
{
return Content("");
}
else
{
return Content("");
}
}
//删除员工
public ActionResult DeleteEmp()
{
//获取主键
int EmpId = int.Parse(Request["EmpId"]);
string sql = "delete from Employee where EmpId=@EmpId";
SqlParameter empId = new SqlParameter("@EmpId", EmpId);
int count = db.Database.ExecuteSqlCommand(sql, empId);
//var emp = db.Employee.Find(EmpId);
//db.Employee.Remove(emp);
//int count = db.SaveChanges();
if (count > 0)
{
return Content("");
}
else
{
return Content("");
}
}