1:MVC学习——MVC+三层架构

三层架构MVC笔记1、

DAL——数据访问层;(专门与数据库交互,增删查改的方法都在这;需引用MODEL层)

BLL——业务逻辑层;(页面与数据库之间的桥梁;需引用DAL、MODEL层)

MODEL——模型层;(一个数据表对应一个Model类,字段属性皆一样,面向对象编程,参数都以MODEL传递;不需引用其他层)

MVC——网页;(一个CONTROLLER对应VIEW下的文件夹,CONTROLLER下的方法对应VIEW该文件夹下的CSHTML;需引用BLL、MODEL层)

 

 

DAL参考代码:

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;
            }
        }
    }
}
View Code

 

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;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("应用程序或数据库出错:" + ex.Message);
            }
            return objAdmin;
        }
    }
}
View Code

 

BLL参考代码:

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;
        }

    }
}
View Code

 

MODEL参考代码:

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; }

    }
}
View Code

 

MVC——Controllers参考代码:

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();
        }
    }
}
View Code

 

MVC——Views——SysAdmin参考代码:

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="loginId" />
            密码:<input type="text" name="loginPwd" />
            <input type="submit" value="登录" />
        </form>
        @if (IsPost && ViewData["AdminInfo"]!=null)
        {
            for (var i = 1; i < 10; i++)
            {
            <p>@i _ @Request["LoginId"] _ @ViewData["AdminInfo"]</p>
            }
        }
    </div>
</body>
</html>

 

posted @ 2019-10-19 09:46  AaronLi  阅读(200)  评论(0编辑  收藏  举报