Three-tier architecture MVC notes 1,
DAL-- data access layer; (specifically interact with the database, check additions are modified in this way; MODEL required reference layer)
BLL-- business logic; (bridge between the pages with the database; required reference DAL, MODEL Layer)
MODEL-- model layer; (a data table corresponding to a Model class field attributes are the same, object-oriented programming, parameters are passed to MODEL; without reference to other layers)
MVC-- page; (CONTROLLER file in a folder corresponding to VIEW, corresponding to the method in CONTROLLER VIEW CSHTML the folder; references required BLL, MODEL Layer)
DAL Reference Code:
Helper——SqlHelper.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.Data; using System.Data.SqlClient; namespace DAL.Helper { class SqlHelper { private static readonly string connString = ConfigurationManager.ConnectionStrings["connString"].ToString(); /// <summary> /// 执行增删改方法 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int Update(string sql) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); try { conn.Open(); return cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 执行返回单一结果查询 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static object GetSingleResult(string sql) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); try { conn.Open(); return cmd.ExecuteScalar(); } catch (Exception ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 执行一个结果集的查询 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static SqlDataReader GetReader(string sql) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); try { conn.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception ex) { conn.Close(); throw ex; } } } }
SysAdminService.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Models; using System.Data; using System.Data.SqlClient; namespace DAL { public class SysAdminService { /// <summary> /// 根据用户名和密码查询 /// </summary> /// <param name="objAdmin"></param> /// <returns></returns> public SysAdmin AdminLogin(SysAdmin objAdmin) { string sql = "SELECT AdminName FROM Admins WHERE LoginId='{0}' AND LoginPwd='{1}'"; sql = string.Format(sql, objAdmin.LoginId, objAdmin.LoginPwd); try { SqlDataReader reader = Helper.SqlHelper.GetReader(sql); if (reader.Read()) { objAdmin.AdminName = reader["AdminName"].ToString(); reader.Close(); } else { objAdmin= null ; } } the catch (Exception EX) { the throw new new Exception ( " application or database error: " + ex.Message); } return objAdmin; } } }
BLL reference code:
SysAdminManager.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Models; using DAL; using System.Web; namespace BLL { public class SysAdminManager { /// <summary> /// 根据用户名和密码查询 /// </summary> /// <param name="objAdmin"></param> /// <returns></returns> public SysAdmin AdminLogin(SysAdmin objAdmin) { objAdmin = new SysAdminService().AdminLogin(objAdmin); if (objAdmin != null) { HttpContext.Current.Session["CurrentAdmin"] = objAdmin; } return objAdmin; } } }
MODEL reference code:
SysAdmin.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Models { /// <summary> /// 管理员类 /// </summary> [Serializable] public class SysAdmin { public string LoginId { get; set; } public string AdminName { get; set; } public string LoginPwd { get; set; } } }
MVC - Controllers Reference Code:
SysAdminController.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Models; using BLL; namespace StudentManagerMVC.Controllers { public class SysAdminController : Controller { // GET: SysAdmin public ActionResult Index() { return View("AdminLogin"); } public ActionResult AdminLogin() { if (string.IsNullOrEmpty(Request.Params["loginId"].ToString()) || string.IsNullOrEmpty(Request.Params["loginPwd"].ToString())) { ViewData["AdminInfo"] = "请输入用户名密码!"; } else { SysAdmin objAdmin = new SysAdmin() { LoginId = Request.Params["loginId"].ToString(), LoginPwd = Request.Params["loginPwd"].ToString() }; objAdmin = new SysAdminManager().AdminLogin(objAdmin); if (objAdmin != null) { ViewData["AdminInfo"] = objAdmin.AdminName; } else { ViewData["AdminInfo"] = null; } } return View(); } } }
MVC - Views - SysAdmin reference code:
AdminLogin.cshtml:
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>AdminLogin</title> </head> <body> <div> <form action="/SysAdmin/AdminLogin" method="post"> 用户名:<input type="text" name=" Password:/>"loginId<input type="text" name="loginPwd" /> <input type="submit" value="登录" /> </form> @ViewData["AdminInfo"] </div> </body> </html>