首先打開vs軟體新建項目創建web中的mvc項目再右擊解決方案創建類庫項目分別創建DAL層和BLL層再把DAL層和BLL層的類重命名在mvc項目中的Models文件夾創建model類在DAL創建ADO.NET實體數據模型後把DAL層中App.Config文件中的鏈接字元串複製到mvc項目的Web.c ...
首先打開vs軟體
新建項目
創建web中的mvc項目
再右擊解決方案創建類庫項目
分別創建DAL層和BLL層再把DAL層和BLL層的類重命名
在mvc項目中的Models文件夾創建model類
在DAL創建ADO.NET實體數據模型後把DAL層中App.Config文件中的鏈接字元串複製到mvc項目的Web.config文件中
DAL層中的類開始打代碼
登錄
/// <summary> /// 登錄 /// </summary> /// <param name="studentname">登錄名</param> /// <param name="studentaddress">是否停用</param> /// <param name="phone">密碼</param> /// <returns></returns> public static int Login(string studentname, string phone) { using (zzqEntities1 db = new zzqEntities1()) { int stu = db.student.Where(s => s.Studentname == studentname && s.Studentaddress =="啟用" && s.phone == phone).Count(); return stu; } }
查詢
/// <summary> /// 查詢 /// </summary> /// <returns></returns> public static List<student> studentSelect() { using (zzqEntities1 db = new zzqEntities1()) { List<student> stu = new List<student>(); stu = db.student.ToList(); return stu; } }
添加
/// <summary> /// 添加 /// </summary> /// <param name="studentname">姓名</param> /// <param name="studentaddress">是否停用</param> /// <param name="phone">密碼</param> /// <returns></returns> public static int Insert(string studentname, string studentaddress, string phone) { using (zzqEntities1 db = new zzqEntities1()) { var stu = new student() { Studentname = studentname, Studentaddress = studentaddress, phone = phone }; db.student.Add(stu); return db.SaveChanges(); } }
刪除
/// <summary> /// 刪除 /// </summary> /// <param name="studentid">編號</param> /// <returns></returns> public static int Delete(int id) { using (zzqEntities1 db = new zzqEntities1()) { var stu = new student() { Studentid = id }; db.student.Attach(stu); db.student.Remove(stu); return db.SaveChanges(); } }
修改
/// <summary> /// 查詢編號 /// </summary> /// <param name="studentid">編號</param> /// <returns></returns> public static List<student> updateSelect(int id) { using (zzqEntities1 db = new zzqEntities1()) { var st = db.student.Where(x => x.Studentid == id).ToList(); return st; } } /// <summary> /// 修改 /// </summary> /// <param name="studentid">編號</param> /// <param name="studentname">姓名</param> /// <param name="studentaddress">是否停用</param> /// <param name="phone">密碼</param> /// <returns></returns> public static int update(int id, string studentname, string studentaddress, string phone) { using (zzqEntities1 db = new zzqEntities1()) { var st = db.student.Where(x => x.Studentid == id).FirstOrDefault(); st.Studentid = id; st.Studentname = studentname; st.Studentaddress = studentaddress; st.phone = phone; return db.SaveChanges(); } }
BLL層
using DAL;
引用DAL層
登錄
/// <summary> /// 登錄 /// </summary> /// <param name="studentname"></param> /// <param name="studentaddress"></param> /// <param name="phone"></param> /// <returns></returns> public static int Login(string studentname, string phone) { try { int count = kaoshiDAL.kaoshidal.Login(studentname, phone); return count; } catch (Exception ex) { throw ex; } }
查詢
/// <summary> /// 查詢 /// </summary> /// <returns></returns> public static List<student> studentSelect() { try { return kaoshiDAL.kaoshidal.studentSelect(); } catch (Exception ex) { throw ex; } }
添加
/// <summary> /// 添加 /// </summary> /// <param name="studentname">姓名</param> /// <param name="studentaddress">是否停用</param> /// <param name="phone">密碼</param> /// <returns></returns> public static int Insert(string studentname, string studentaddress, string phone) { try { return kaoshiDAL.kaoshidal.Insert(studentname,studentaddress,phone); } catch (Exception ex) { throw ex; } }
刪除
/// <summary> /// 刪除 /// </summary> /// <param name="studentid"></param> /// <returns></returns> public static int Delete(int id) { try { return kaoshiDAL.kaoshidal.Delete(id); } catch (Exception ex) { throw ex; } }
修改
/// <summary> /// 查詢編號 /// </summary> /// <param name="studentid"></param> /// <returns></returns> public static List<student> updateSelect(int id) { try { return kaoshiDAL.kaoshidal.updateSelect(id); } catch (Exception ex) { throw ex; } } /// <summary> /// 修改 /// </summary> /// <param name="studentid"></param> /// <param name="studentname"></param> /// <param name="studentaddress"></param> /// <param name="phone"></param> /// <returns></returns> public static int update(int id, string studentname, string studentaddress, string phone) { try { return kaoshiDAL.kaoshidal.update(id, studentname, studentaddress, phone); } catch (Exception ex) { throw ex; } }
mvc項目中的Models文件夾的model類
using DAL;
using BLL;
這裡引用DAL層和BLL層
登錄
/// <summary> /// 登錄 /// </summary> /// <param name="studentname"></param> /// <param name="studentaddress"></param> /// <param name="phone"></param> /// <returns></returns> public static int Login(string studentname, string phone) { try { int count= kaoshiBLL.kaoshibll.Login(studentname, phone); return count; } catch (Exception ex) { throw ex; } }
查詢
/// <summary> /// 查詢 /// </summary> /// <returns></returns> public static List<student> studentSelect() { try { return kaoshiBLL.kaoshibll.studentSelect(); } catch (Exception ex) { throw ex; } }
添加
/// <summary> /// 添加 /// </summary> /// <param name="studentname">姓名</param> /// <param name="studentaddress">是否停用</param> /// <param name="phone">密碼</param> /// <returns></returns> public static int Insert(string studentname, string studentaddress, string phone) { try { return kaoshiBLL.kaoshibll.Insert(studentname,studentaddress,phone); } catch (Exception ex) { throw ex; } }
刪除
/// <summary> /// 刪除 /// </summary> /// <param name="studentid"></param> /// <returns></returns> public static int Delete(int id) { try { return kaoshiBLL.kaoshibll.Delete(id); } catch (Exception ex) { throw ex; } }
修改
/// <summary> /// 查詢編號 /// </summary> /// <param name="studentid"></param> /// <returns></returns> public static List<student> updateSelect(int id) { try { return kaoshiBLL.kaoshibll.updateSelect(id); } catch (Exception ex) { throw ex; } } /// <summary> /// 修改 /// </summary> /// <param name="studentid"></param> /// <param name="studentname"></param> /// <param name="studentaddress"></param> /// <param name="phone"></param> /// <returns></returns> public static int update(int id, string studentname, string studentaddress, string phone) { try { return kaoshiBLL.kaoshibll.update(id, studentname, studentaddress, phone); } catch (Exception ex) { throw ex; } }
在mvc項目中的Controllers文件夾創建Home控制器
using Kaoshi.Models;
using kaoshiDAL;
using kaoshiBLL;
登錄
/// <summary> /// 登錄 /// </summary> /// <returns></returns> public ActionResult Login() { return View(); } public ActionResult ALogin(string studentname, string phone) { int count = kaoshiModel.Login(studentname, phone); if (count >0) { return Content("<script>alert('登錄成功');window.location.href='/Home/Index';</script>"); } else { return Content("<script>alert('登錄失敗');window.location.href='/Home/Login';</script>"); } }
查詢
/// <summary> /// 查詢 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Index() { List<student> stu = kaoshiModel.studentSelect(); return View(stu); }
註冊
/// <summary> /// 註冊 /// </summary> /// <returns></returns> public ActionResult Zhuce() { return View(); } [HttpPost] public ActionResult Azhuce(string studentname, string studentaddress, string phone) { int st = kaoshiModel.Insert(studentname,studentaddress,phone); if (st > 0) { return Content("<script>alert('註冊成功');window.location.href='/Home/Login';</script>"); } else { return Content("<script>alert('註冊失敗');window.location.href='/Home/Zhuce';</script>"); } }
刪除
/// <summary> /// 刪除 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Delete(int id) { int st = kaoshiModel.Delete(id); if (st > 0) { return Content("<script>alert('刪除成功');window.location.href='/Home/Index';</script>"); } else { return Content("<script>alert('刪除失敗');window.location.href='/Home/Index';</script>"); } }
修改
//修改 public ActionResult Update(int id) { List<student> li = kaoshiModel.updateSelect(id); return View(li); } public ActionResult updatestudent(int id, string studentname, string studentaddress, string phone) { int st = kaoshiModel.update(id, studentname, studentaddress, phone); if (st>0) { return Content("<script>alert('修改成功');window.location.href='/Home/Index';</script>"); } else { return Content("<script>alert('修改失敗');window.location.href='/Home/Index';</script>"); } }
Index視圖
@{ ViewBag.Title = "Index"; } <script type="text/javascript"> function Update(id) { location.href = "/Home/Update/" + id; } function del(id) { if (confirm("是否刪除?")) { location.href = '/Home/Delete/' + id; } } </script> <a href="/Home/Login">登錄</a> <table> <tr> <th>姓名<th> <th>密碼</th> <th>是否停用</th> <th colspan="2">操作</th> </tr> @foreach (var item in Model) { <tr> <td>@item.Studentname</td> <td>@item.phone</td> <td>@item.Studentaddress</td> <td><a href="#" onclick="Update(@item.Studentid)">修改</a></td> <td><a href="#" onclick="del(@item.Studentid)">刪除</a></td> </tr> } </table>
Login視圖
@{ ViewBag.Title = "Login"; } <h2>登錄</h2> @using (Html.BeginForm("ALogin", "Home", FormMethod.Post)) { <input type ="text" name="studentname" placeholder="請輸入姓名"/> <input type ="password" name="phone" placeholder="請輸入密碼"/> <button type="submit" class="blue" >登錄</button> <a href="/Home/Zhuce">註冊</a> }
Update視圖
@{ ViewBag.Title = "Update"; } @using (Html.BeginForm("updatestudent", "Home", FormMethod.Post)) { <table> @foreach (var item in Model) { <tr> <td>編號:</td> <td> <input type="text" value="@item.Studentid" name="id" readonly="readonly"/> </td> </tr> <tr> <td>姓名:</td> <td> <input type="text" value="@item.Studentname" name="studentname" readonly="readonly"/> </td> </tr> <tr> <td>是否停用:</td> <td> <input type="text" value="@item.Studentaddress" name="studentaddress"/> </td> </tr> <tr> <td>密碼:</td> <td> <input type="text" value="@item.phone" name="phone" readonly="readonly"/> </td> </tr> <tr> <td colspan="2"> <input type="submit" id="btn" value="修改" /></td> </tr> } </table> }
Zhuce視圖
@{ ViewBag.Title = "Zhuce"; } @using (Html.BeginForm("Azhuce", "Home", FormMethod.Post)) { <table style="width: 100%;"> <tr> <td>姓名:</td> <td> <input type="text" placeholder="請輸入姓名" name="studentname" /> </td> </tr> <tr> <td>狀態:</td> <td> <select name="studentaddress"> <option value="啟用" selected="selected">啟用</option> <option value="禁用">禁用</option> </select> </td> </tr> <tr> <td>密碼:</td> <td> <input type="text" placeholder="請輸入密碼" name="phone" /> </td> </tr> <tr> <td colspan="2"> <input id="btn" type="submit" value="註冊" /> </td> </tr> </table> }