这里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")

 

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

  1. MVC与EasyUI结合增删改查

    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查   在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的 ...

  2. MVC无限级分类02,增删改查

    继上一篇"MVC无限级分类01,分层架构,引入缓存,完成领域模型与视图模型的映射",本篇开始MVC无限级分类的增删改查部分,源码在github. 显示和查询 使用datagrid显 ...

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

    一.存储值 eg:登录一个页面,在进入这个页面之前你怎么知道它登没登录呢?[在登录成功之后我们把状态保存起来] 存储值得方式有两种,一种是cookie,一种是session 1.1区别: 代码: if ...

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

    Mvc模式设计: 视图:添加界面(addUser.jsp),修改界面(updateUser.jsp),显示页面(allUser.jsp) 控制器:添加信息控制器(AddUserServlet),修改信 ...

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

    系列目录 文章于2016-12-17日重写 在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的代码已经给出,里面包含了增删改,大家可以下载下来看下. 这讲主要是,制作漂亮的工具栏,虽 ...

  6. MVC 入门 自动生成 增删改查所有功能

    MVC现在版本已经是5了   EF现在最新的应该是6.0.2了 开发工具是 Visual Studio2013 数据库是 SQL Server 2012 这些需要.NET Framework4.5 的 ...

  7. MVC 中aspx的增删改查

    先看总体结构 LInQ #pragma warning disable 1591 //--------------------------------------------------------- ...

  8. MVC中使用EF增删改查,简单的例子

    //这个是分页数据和总页数类 public class SummaryBase<TModel> { public SummaryBase(); public IList<TModel ...

  9. ASP.NET+MVC+EntityFramework快速实现增删改查

    本教程已经录制视频,欢迎大家观看我在CSDN学院录制的课程:http://edu.csdn.net/lecturer/944

随机推荐

  1. Creating Excel files with Python and XlsxWriter——Introduction

    XlsxWriter 是用来写Excel2007版本以上的xlsx文件的Python模块. XlsxWriter 在供选择的可以写Excel的Python模块中有自己的优缺点. #---------- ...

  2. windows 8.1 启用hyper-v导致vmware 无法使用的问题解决方案(兼顾WP8.1模拟器和vmware)

    最近搭建了windows phone 8.1开发环境,为了开机就可以进行WP8.1开发,就使用了 bcdedit /set {BCD ID} hypervisorlaunchtype auto 命令将 ...

  3. spring jpa方法关键字转成sql

    The following table describes the keywords supported for JPA and what a method containing that keywo ...

  4. Mongodb中的 原子性 隔离性

    读写锁 Mongodb使用读写锁来来控制并发操作: 当进行读操作的时候会加读锁,这个时候其他读操作可以也获得读锁.但是不能或者写锁. 当进行写操作的时候会加写锁,这个时候不能进行其他的读操作和写操作. ...

  5. web安全类

    web安全类主要分为两个部分:CSRF和XSS 一.CSRF 基本概念:CSRF,通常称为跨站请求伪造,英文名Cross-site request forgery 缩写为CSRF; 怎么防御 1.To ...

  6. Navicat premium 12破解版

    下载Navicat  Premium 12和破解补丁Navicat_Keygen_Patch,底部有下载地址.下载之后安装Navicat,安装成功后先不要打开,然后打开破解补丁,破解补丁不需要安装,双 ...

  7. Halcon Visinpro 破解版

    目前测试过的破解版资料: halcon10    可用已测   完美破解 halcon12   可用已测   完美破解 halcon13   可用已测   完美破解 halcon17   可用已测   ...

  8. VB.net中合并word中的表格

    软帝国产品的互兼容性使得我们采用vb.net编程语言操作Microsoft Word文档变得相当容易.针对本文的主题,网络上已经有很多大牛博客做了详细的介绍,基本的我就不再赘述,只是自己在做项目的时候 ...

  9. MacBook Home End

    For the Home command, press down the Fn + Left Arrow keystroke combination. For the End command, pre ...

  10. Flink开发环境搭建(maven)

    1.下载scala sdk http://www.scala-lang.org/download/ 直接到这里下载sdk,(https://downloads.lightbend.com/scala/ ...