关于MVC工厂模式的增删改查sql存储过程

这里MVC中用到了反射,工厂,泛型,接口

在搭建框架的时候,除了MVC的三层以外,还有泛型的接口层和工厂层

下面是dal层调用sql存储过程,增删改查,dal层继承了接口层,实现了接口层里面的方法

  1 namespace DAL
  2 {
  3     public class DalHouse : IHouse
  4     {
  5         public int Add(HouseInfo m)
  6         {
  7             string sql = "pro_add";
  8             SqlParameter eid = new SqlParameter("@eid", m.Eid);
  9             SqlParameter bid = new SqlParameter("@bid", m.Bid);
 10             SqlParameter floornum = new SqlParameter("@floornum", m.FloorNum);
 11             SqlParameter roomnum = new SqlParameter("@roomnum", m.RoomNum);
 12             SqlParameter spaces = new SqlParameter("@spaces", m.Spaces);
 13             SqlParameter ads = new SqlParameter("@ads", m.Addresses);
 14             SqlParameter tid = new SqlParameter("@tid", m.Tid);
 15             SqlParameter sids = new SqlParameter("@sids", m.Sids);
 16             SqlParameter uid = new SqlParameter("@uid", m.Uid);
 17             SqlParameter[] paras = new SqlParameter[] { eid, bid, floornum, roomnum, spaces, ads, tid, sids, uid };
 18             int i = DbHelperSQL.ExecuteNonQuery(DbHelperSQL.ConnB2c, CommandType.StoredProcedure, sql, paras);
 19             return i;
 20         }
 21
 22         public int Delete(string id)
 23         {
 24             string sql = "pro_delete";
 25             SqlParameter pid = new SqlParameter("@id", id);
 26             SqlParameter[] paras = new SqlParameter[] { pid };
 27             int i = DbHelperSQL.ExecuteNonQuery(DbHelperSQL.ConnB2c, CommandType.StoredProcedure, sql, paras);
 28             return i;
 29         }
 30
 31         public HouseInfo SelectById(int id)
 32         {
 33             string sql = "pro_selectbyid";
 34             SqlParameter pid = new SqlParameter("@id",id);
 35             SqlParameter[] paras = new SqlParameter[] { pid};
 36            DataTable dt= DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c,CommandType.StoredProcedure,sql,paras);
 37             List<HouseInfo> info = JsonConvert.DeserializeObject<List<HouseInfo>>(JsonConvert.SerializeObject(dt));
 38             return info[0];
 39         }
 40
 41         public PageList ShowAll(DataModel d)
 42         {
 43             string sql = "pro_ShowAll";
 44             SqlParameter paraEid = new SqlParameter("@Estateid", d.Eid);
 45             SqlParameter paraBid = new SqlParameter("@BuildingId", d.Bid);
 46             SqlParameter paraTid = new SqlParameter("@Type", d.Tid);
 47             SqlParameter paraSid = new SqlParameter("@State", d.Sids);
 48             SqlParameter paraAddress = new SqlParameter("@Address", d.Addresses);
 49             SqlParameter paraSize = new SqlParameter("@size", d.Size);
 50             SqlParameter paraIndex = new SqlParameter("@index", d.Index);
 51             SqlParameter count = new SqlParameter("@totalCount", SqlDbType.Int);
 52             count.Direction = ParameterDirection.Output;
 53             SqlParameter page = new SqlParameter("@totalPage", SqlDbType.Int);
 54             page.Direction = ParameterDirection.Output;
 55             SqlParameter[] paras = new SqlParameter[] { paraEid, paraBid, paraTid, paraSid, paraAddress, paraSize, count, paraIndex, page };
 56            DataTable dt= DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c,CommandType.StoredProcedure,sql,paras);
 57             PageList p = new PageList();
 58             p.TotalCount = Convert.ToInt32(count.Value);
 59             p.TotalPage = Convert.ToInt32(page.Value);
 60             p.House = JsonConvert.DeserializeObject<List<HouseInfo>>(JsonConvert.SerializeObject(dt));
 61             return p;
 62         }
 63
 64         public List<Building> ShowBuilding()
 65         {
 66             string sql = "select * from Building";
 67              DataTable dt= DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c,CommandType.Text,sql);
 68             return   JsonConvert.DeserializeObject<List<Building>>(JsonConvert.SerializeObject(dt));
 69
 70         }
 71
 72         public List<BuildStruct> ShowBuildStruct()
 73         {
 74             string sql = "select * from BuildStruct";
 75             DataTable dt = DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c, CommandType.Text, sql);
 76             return JsonConvert.DeserializeObject<List<BuildStruct>>(JsonConvert.SerializeObject(dt));
 77         }
 78
 79         public List<HouseEatate> ShowEstate()
 80         {
 81             string sql = "select * from HouseEstate";
 82             DataTable dt = DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c, CommandType.Text, sql);
 83             return JsonConvert.DeserializeObject<List<HouseEatate>>(JsonConvert.SerializeObject(dt));
 84         }
 85
 86         public List<States> ShowStates()
 87         {
 88             string sql = "select * from States";
 89             DataTable dt = DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c, CommandType.Text, sql);
 90             return JsonConvert.DeserializeObject<List<States>>(JsonConvert.SerializeObject(dt));
 91         }
 92
 93         public List<HouseType> ShowType()
 94         {
 95             string sql = "select * from HouseType";
 96             DataTable dt = DbHelperSQL.ExecuteDataTable(DbHelperSQL.ConnB2c, CommandType.Text, sql);
 97             return JsonConvert.DeserializeObject<List<HouseType>>(JsonConvert.SerializeObject(dt));
 98         }
 99
100         public int Update(HouseInfo m)
101         {
102             string sql = "pro_update";
103             SqlParameter id = new SqlParameter("@id", m.Id);
104             SqlParameter eid = new SqlParameter("@eid",m.Eid);
105             SqlParameter bid = new SqlParameter("@bid", m.Bid);
106             SqlParameter floornum = new SqlParameter("@floornum", m.FloorNum);
107             SqlParameter roomnum = new SqlParameter("@roomnum", m.RoomNum);
108             SqlParameter spaces = new SqlParameter("@spaces", m.Spaces);
109             SqlParameter ads = new SqlParameter("@ads", m.Addresses);
110             SqlParameter tid = new SqlParameter("@tid", m.Tid);
111             SqlParameter sids = new SqlParameter("@sids", m.Sids);
112             SqlParameter uid = new SqlParameter("@uid", m.Uid);
113             SqlParameter[] paras = new SqlParameter[] {id,eid,bid,floornum,roomnum,spaces,ads,tid,sids,uid };
114            int i= DbHelperSQL.ExecuteNonQuery(DbHelperSQL.ConnB2c,CommandType.StoredProcedure,sql,paras);
115             return i;
116
117         }
118     }
119 }

这是controller与前台交互,控制器里面的方法需要注意的是下拉的绑定,还有分部视图的使用

我用的是强类型视图,所以视图页面和控制器里面参数的变量名要一致,否则控制器中的方法接受不到值的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAL;
using IDAL;
using Model;
using Factory;
using BLL;
namespace EF_House.Controllers
{
    public class HouseController : Controller
    {

        // GET: House
        public ActionResult Index(int index=1,int size=3)
        {
            DataModel d = new DataModel();
            d.Bid = 0;
            d.Eid = 0;
            d.Sids = 0;
            d.Tid = 0;
            d.Addresses = "";
            d.Index = index;
            d.Size = size;
            BllHouse bll = new BllHouse();
            PageList info = bll.ShowAll(d);
            ViewBag.totalpage = info.TotalPage;
            ViewBag.totalcount = info.TotalCount;
            ViewBag.index = d.Index;
            //下拉绑定
            ViewBag.Estate = new SelectList(bll.ShowEatate(), "Id", "Ename");
            ViewBag.HouseType = new SelectList(bll.ShowType(), "Id", "TypeName");
            ViewBag.States = new SelectList(bll.ShowStates(), "Id", "Sname");
            ViewBag.Building = new SelectList(bll.ShowBuilding(), "Bid", "Bname");
            return View(info.House);
        }
        public ActionResult Search(string Addresses="",int Bid=0,int Eid=0,int Sids=0,int Tid=0,int index=1,int size=3)
        {
            if (Addresses == null)
            {
                Addresses = "";
            }
            ViewBag.bid = Bid;
            ViewBag.eid = Eid;
            ViewBag.sids = Sids;
            ViewBag.tid = Tid;
            ViewBag.ads = Addresses;
            DataModel d = new DataModel();
            d.Bid = Bid;
            d.Eid = Eid;
            d.Sids = Sids;
            d.Tid = Tid;
            d.Addresses = Addresses;
            d.Index = index;
            d.Size = size;

            BllHouse bll = new BllHouse();
            PageList p = bll.ShowAll(d);
            ViewBag.totalcount = p.TotalCount;
            ViewBag.totalpage = p.TotalPage;
            ViewBag.index = d.Index;
            //下拉绑定
            ViewBag.Estate = new SelectList(bll.ShowEatate(), "Id", "Ename");
            ViewBag.HouseType = new SelectList(bll.ShowType(), "Id", "TypeName");
            ViewBag.States = new SelectList(bll.ShowStates(), "Id", "Sname");
            ViewBag.Building = new SelectList(bll.ShowBuilding(), "Bid", "Bname");

            return PartialView("Search",p.House);
        }
        public int DeleteAll(string id)
        {
            BllHouse bll = new BllHouse();
            int i = bll.Delete(id);
            return i;
        }
        public void Delete(string id)
        {
            BllHouse bll = new BllHouse();
            int i = bll.Delete(id);
            if (i > 0)
            {
                Response.Write("<script>alert(‘删除成功‘);location.href=‘/house/index/1‘</script>");
            }
            else
            {
                Response.Write("<script>alert(‘删除失败‘)</script>");
            }
        }
        public ActionResult Update(int id)
        {
            BllHouse bll = new BllHouse();
            ViewBag.Estate = new SelectList(bll.ShowEatate(), "Id", "Ename");
            ViewBag.HouseType = new SelectList(bll.ShowType(), "Id", "TypeName");
            ViewBag.States = new SelectList(bll.ShowStates(), "Id", "Sname");
            ViewBag.Building = new SelectList(bll.ShowBuilding(), "Bid", "Bname");
            ViewBag.Struct = new SelectList(bll.ShowBuildStruct(), "Id", "Uname");

            HouseInfo h = bll.SelectById(id);

            return View(h);
        }
        [HttpPost]
        public void Update(HouseInfo h)
        {
            BllHouse bll = new BllHouse();
            int i = bll.Update(h);
            if (i > 0)
            {
                Response.Write("<script>alert(‘修改成功‘);location.href=‘/house/index/1‘</script>");
            }
            else
            {
                Response.Write("<script>alert(‘修改失败‘)</script>");
            }
        }
        public ActionResult Add()
        {
            BllHouse bll = new BllHouse();
            ViewBag.Estate = new SelectList(bll.ShowEatate(), "Id", "Ename");
            ViewBag.HouseType = new SelectList(bll.ShowType(), "Id", "TypeName");
            ViewBag.States = new SelectList(bll.ShowStates(), "Id", "Sname");
            ViewBag.Building = new SelectList(bll.ShowBuilding(), "Bid", "Bname");
            ViewBag.Struct = new SelectList(bll.ShowBuildStruct(), "Id", "Uname");
            return View();
        }
        [HttpPost]
        public ActionResult Add(HouseInfo h)
        {
            BllHouse bll = new BllHouse();
            int i = bll.Add(h);
            if (i > 0)
            {
                return Content("<script>alert(‘添加成功‘);location.href=‘/house/index/1‘</script>");
            }
            else
            {
                return Content("<script>alert(‘添加失败‘)</script>");
            }

        }
    }
}

这里需要说一下,jquery写的全选和批删的方法,详情看代码

 <script>
//全选
        $(function () {
            $("#CheckAll").click(function () {
                if (this.checked) {
                    $("input[name=‘check‘]").each(function () {
                        this.checked = true;
                    })
                }

                else {
                    $("input[name=‘check‘]:checked").each(function () {
                        this.checked = false;
                    })
                }

            })
        })
        function DelAll() {
            var check = [];
            $("input[name=‘check‘]:checked").each(function () {
                check.push($(this).val());
            })
            if (check == 0) {
                alert("请先进行选择");
            }
            else {
                $.ajax({
                    type: "post",
                    url: "/house/deleteall",
                    data: { id: check.toString() },
                    success: function (a) {
                        if (a > 0) {
                            alert("批量删除成功!");
                            location.reload();
                        }
                    }
                })
            }

        }
    </script>

基本的页面布局,这里就不展示了,

还需要说的就是使用强类型视图,在修改的时候,

需要使用@html.Hidden("Id")

原文地址:https://www.cnblogs.com/dxx117/p/9525636.html

时间: 2024-10-29 04:39:32

关于MVC工厂模式的增删改查sql存储过程的相关文章

sqlHelper做增删改查,SQL注入处理,存储值,cookie,session

一.存储值 eg:登录一个页面,在进入这个页面之前你怎么知道它登没登录呢?[在登录成功之后我们把状态保存起来] 存储值得方式有两种,一种是cookie,一种是session 1.1区别: 代码: if (SqlHelper.Exists(sSql, para)) { //cookie保存状态 if (chkRPwd.Checked) { Response.Cookies["name"].Expires = DateTime.Now.AddMinutes(1);//设置过期时间 //删除

使用jdbc实现简单的mvc模式的增删改查

Mvc模式设计: 视图:添加界面(addUser.jsp),修改界面(updateUser.jsp),显示页面(allUser.jsp) 控制器:添加信息控制器(AddUserServlet),修改信息控制器(UpdateUserServlet),删除信息控制器(DeleteUserServlet),显示信息控制器(FindAllUserServlet) 模型:userbean 数据库层:DBBean 总体设计: 添加信息模块:用户通过添加信息界面(addUser.jsp)提交表单,提交的信息有

构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查

在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的代码已经给出,里面包含了增删改,大家可以下载下来看下.这讲主要是,制作漂亮的工具栏,虽然easyui的datagrid已经自带可以设置工具栏,我们还是要提取出来,为以后权限控制做更好的准备. 前端代码没有逻辑结果,这也许是我写代码以来写得最轻松的,但也是最繁琐的,因为美工我不是强项,每一次调整都非常的困难,最后我把他调成了这样了: 看得过去的鼓掌一下.样式已经包含在附加代码中了. 大家只要加入以下HTML代码到index上就可以

一个基本的MVC模式的增删改查

Model:Employee.java View:listEmp.jsp   addEmp.jsp   modifyEmp.jsp Controler:ActionServlet Dao:BaseDao.java(与DBUtil功能一致,可以让子类dao继承)   EmpDao.java 本案例采用一个Servlet控制器接收请求,所有请求格式为...*.do,servlet根据.do前面与"/"后面的内容判断请求类型. 执行步骤演示: 浏览器发送请求:request:http://l

MVC 三步完成增删改查设计

1.添加模型类: public class Product { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public decimal Price { get; set; } } 2.添加上下文类: public class ProductContext : DbContext { public ProductContext() : base(

ThinkPHP--ActiveReocrd 模式(增删改查)

ActiveReocrd 模式 这种模式最大的特别就是简化了CURD 的操作,并且采用对象化的操作方式,便于使用 和理解. //添加一条数据 $user = M('User'); $user->user = '火影忍者'; $user->email = '[email protected]'; $user->date = date('Y-m-d H:i:s'); $user->add(); //结合create $user = M('User'); $user->create

ASP.NET MVC之Entity Framework增删改查

一.EntityFramework简介 ORM(Object-Relation-Mapping):对象关系映射,主要实现基于面向对象方式操作数据库的各种方法,是一种框架技术.长期以来,C#OOP和数据库操作一直处于分离状态.C#最后的操作都要转换成普通的SQL语句,从开发角度来讲,这种转换工作,对于快速开发来讲,效率会有一定影响.ORM出来以后:这种转换工作,其实由ORM框架本身完成.完成基于对象操作数据. .NET中的ORM框架:.NHibernate.MyBatis.NET.LINQ to

转载-增删改查sql语句语法

一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert into Strdents (姓名,性别,出生日期) values ('王伟华','男','1983/6/15')  注意:如果省略表名,将依次插入所有列 2.使用insert,select语句将现有表中的 数据添加到已有的新表中 语法:insert into <已有的新表> <列名> select <

MySQL数据库增删改查SQL语句(2018整理集合大全)

查看数据库 show databases; 使用数据库 use 数据库名;创建数据库 CREATE DATABASE 数据库名;删除数据库 DROP DATABASE 数据库名;创建表 create table 表名(    列名1 类型(长度) [约束],    列名2 类型(长度) [约束],    ……);长度区别int类型带长度:不影响存取值,即使设定的值超出了长度的范畴,也能存,如果没有达到设定的长度,则使用空格自动填充到设定的长度char类型:不可变字符,设定的长度就是规定当前字段能