1. 程式人生 > >關於MVC工廠模式的增刪改查sql存儲過程

關於MVC工廠模式的增刪改查sql存儲過程

var http 添加 rec 接受 ring 變量 index ext

這裏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 }
View Code 這是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>");
            }
            
        }
    }
}
View Code

這裏需要說一下,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>
View Code

基本的頁面布局,這裏就不展示了,

還需要說的就是使用強類型視圖,在修改的時候,

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

關於MVC工廠模式的增刪改查sql存儲過程