bob体育app主攻ASP.NET.4.5.1 MVC5.0之重生:系统角色与权限(二)

2019-12-14 09:00栏目:编程
TAG:

bob体育平台,代码:

系统角色篇

一、业务需求:

bob体育平台 1bob体育平台 2

数据结构

利用三层架构实现对数据库数据的分页功能和点击每个页码实现不同分页面之间的跳转,效果如下图所示:

private static ImageCodecInfo GetImageCodecInfo(ImageFormat imageFormat)
{
    ImageCodecInfo[] imageCodecInfoArr = ImageCodecInfo.GetImageDecoders();
    foreach (ImageCodecInfo imageCodecInfo in imageCodecInfoArr)
    {
        if (imageCodecInfo.FormatID == imageFormat.Guid)
        {
            return imageCodecInfo;
        }
    }
    return null;
}

bob体育平台 3

bob体育平台 4

View Code

用户管理 

bob体育平台 5

代码:

bob体育平台 6

bob体育平台 7

bob体育平台 8bob体育平台 9

Controller代码

二、三层结构代码详细示例

MemoryStream ms = HttpUtil.HttpDownloadFile(url);
Bitmap bmp = new Bitmap(ms);

EncoderParameters encoderParameters = new EncoderParameters(1);
EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
encoderParameters.Param[0] = encoderParameter;

MemoryStream msCompress = new MemoryStream();
bmp.Save(msCompress, GetImageCodecInfo(ImageFormat.Jpeg), encoderParameters);
Bitmap bmpCompress = new Bitmap(msCompress);
bmpCompress.Save(path);
bmp.Save(path2);

msCompress.Close();
ms.Close();

bob体育平台 10bob体育平台 11

1、表现层代码

bob体育app,View Code

  1 public class SystemUserController : Controller
  2     {
  3         //public void Log()
  4         //{
  5         //    string meg = "";
  6         //    int user = int.Parse(CookieHelper.GetValue("SysUserID"));
  7         //    string ip = IPHelper.GetUserIp;
  8         //    foreach (var item in RouteData.Values)
  9         //    {
 10         //        meg += item.Key.ToUpper() + ":" + item.Value + "|";
 11         //    }
 12         //    AuditLogConfig.InsertAuditLog(meg, user, ip);
 13         //}
 14         SystemUserRepository sysuserrepository = new SystemUserRepository();
 15         RoleRepository roleryrepository = new RoleRepository();
 16         DepartmentRepository departmentryrepository = new DepartmentRepository();
 17         JobRepository jobryrepository = new JobRepository();
 18         #region 列表页
 19         /// <summary>
 20         /// 列表页
 21         /// </summary>
 22         /// <param name="page">当前页</param>
 23         /// <returns></returns>
 24         [Authorize]
 25         public ActionResult Index(int? page)
 26         {
 27             ViewBag.ControllerName=RouteData.Values["controller"].ToString().ToLower();
 28             CookieHelper.Del("LogName");
 29             var source = sysuserrepository.GetPageModelList(10, page ?? 1, -1);
 30             return View(source);
 31         }
 32         #endregion
 33 
 34         #region 添加管理员
 35         /// <summary>
 36         /// 添加页
 37         /// </summary>
 38         /// <param name="model">管理员实体类</param>
 39         /// <returns></returns>
 40         [Authorize]
 41         public ActionResult Add(SystemUser model)
 42         {
 43             ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
 44 
 45             var rolelist = roleryrepository.GetModelListByState(1);
 46             ViewBag.RoleID = SelectListExtension.ToSelectList(rolelist, d => d.RoleName, d => d.RoleID.ToString());
 47 
 48             var departmentlist = departmentryrepository.GetModelListByState(1);
 49             ViewBag.DepartmentID = SelectListExtension.ToSelectList(departmentlist, d => d.DepartmentName, d => d.DepartmentID.ToString());
 50 
 51             var joblist = jobryrepository.GetModelListByState(1);
 52             ViewBag.JobID = SelectListExtension.ToSelectList(joblist, d => d.JobName, d => d.JobID.ToString());
 53 
 54             // ViewData["State"] = SelectListHelper.GetIsOrNoSelectList(0);
 55             return View();
 56         }
 57 
 58         /// <summary>
 59         /// 添加事件
 60         /// </summary>
 61         /// <param name="model">实体类</param>
 62         /// <param name="fc"></param>
 63         /// <returns></returns>
 64         [Authorize, HttpPost, ValidateInput(false)]
 65         public ActionResult Add(SystemUser model, FormCollection fc)
 66         {
 67             model.State = 1;
 68             model.CreateTime = DateTime.Now;
 69             sysuserrepository.SaveOrEditModel(model);
 70             return RedirectToAction("Index");
 71         }
 72         #endregion
 73 
 74         #region 删除
 75         /// <summary>
 76         /// 删除
 77         /// </summary>
 78         /// <param name="id">删除编号</param>
 79         /// <returns></returns>
 80         [Authorize]
 81         public ActionResult Delete(int id)
 82         {
 83             try
 84             {
 85                 var model = sysuserrepository.GetModel(id);
 86                 if (model != null)
 87                 {
 88                     sysuserrepository.DeleteModel(id);
 89                     return RedirectToAction("Index");
 90                 }
 91                 else
 92                     return View("404");
 93                 //throw new Exception("方法出错!");
 94 
 95             }
 96             catch (Exception ex)
 97             {
 98                 Log4netHelper.InvokeErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "删除方法出错-Author:ryanding IP地址[" + IPHelper.GetUserIp + "]" + "操作人ID:" + CookieHelper.GetValue("SystemUserID"), ex);
 99                 return View("404");
100             }
101            
102            
103         }
104         #endregion
105 
106         #region 查询
107         [Authorize]
108         public ActionResult Search(int? page)
109         {
110             string logname = "";
111             if (page == null)
112             {
113                 logname = Request.Form["KeyWord"];
114                 CookieHelper.SetObj("KeyWord", 0, logname);
115             }
116             else
117                 logname = CookieHelper.GetValue("KeyWord");
118 
119             var query = QueryBuilder.Create<SystemUser>().Like(d => d.LogName, logname);
120             var source = sysuserrepository.GetModelList().Where(query.Expression);
121             return View(new Pager<SystemUser>(source, page ?? 1));
122         }
123 
124         #endregion
125 
126         #region 编辑
127         [Authorize]
128         public ActionResult Edit(int id)
129         {
130             ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
131             var model = sysuserrepository.GetModel(id);
132             if (model != null)
133             {
134                 var rolelist = roleryrepository.GetModelListByState(1);
135                 ViewBag.RoleID = SelectListExtension.ToSelectList(rolelist, d => d.RoleName, d => d.RoleID.ToString(),model.RoleID);
136 
137                 var departmentlist = departmentryrepository.GetModelListByState(1);
138                 ViewBag.DepartmentID = SelectListExtension.ToSelectList(departmentlist, d => d.DepartmentName, d => d.DepartmentID.ToString(), model.DepartmentID);
139 
140                 var joblist = jobryrepository.GetModelListByState(1);
141                 ViewBag.JobID = SelectListExtension.ToSelectList(joblist, d => d.JobName, d => d.JobID.ToString(), model.JobID);
142                 ViewData["State"] = SelectListHelper.GetStateSelectList(model.State);
143                 return View(model);
144             }
145             else
146                 return View("404");
147         }
148 
149         [Authorize, AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
150         public ActionResult Edit(int id, FormCollection fc)
151         {
152             var model = sysuserrepository.GetModel(id);
153 
154             if (model != null)
155             {
156                 string password = model.Password;
157                 if (Request.Form["PassWord"] != "")
158                 {
159                     UpdateModel(model);
160                     sysuserrepository.SaveOrEditModel(model);
161                 }
162                 else
163                 {
164                     UpdateModel(model);
165                     model.Password = password;
166                     sysuserrepository.SaveOrEditModel(model);
167                 }
168                 return RedirectToAction("index");
169             }
170             else
171                 return View("404");
172         }
173         #endregion
174 
175         #region 用户名是否可用
176         //检测用户名是否可用
177         [HttpPost]
178         public ActionResult GetUsers(string param, string name)
179         {
180             if (sysuserrepository.IsExist(param))
181                 return Content("y");
182             else
183                 return Content("用户名已存在");
184         }
185         #endregion
186 
187 
188         public bool Login()
189         {
190             try
191             {
192                 //TODO:
193                 throw new Exception("方法出错!");
194 
195             }
196             catch (Exception ex)
197             {
198                 Log4netHelper.InvokeErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "登录方法Login出错-Author:ryanding IP地址[" + IPHelper.GetUserIp + "]" + "操作人ID:" + CookieHelper.GetValue("SystemUserID"), ex);
199                 return false;
200             }
201             return true;
202         }
203     }

bob体育平台 12bob体育平台 13

 

View Code

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo3.aspx.cs" Inherits="Chapter05.Demo3" %> 2  3 <!DOCTYPE html> 4  5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8     <title></title> 9     <%--引入css样式表--%>10     <link href="PageBar.css" rel="stylesheet" />11 </head>12 <body>13     <form id="form1" runat="server">14         <div>15             <table>16 17                 <asp:Repeater ID="Repeater1" runat="server">18                     <HeaderTemplate>19                         <tr>20                             <th>登录名</th>21                             <th>密码</th>22                             <th>是否激活</th>                           23                         </tr>24                     </HeaderTemplate>25                     <ItemTemplate>26                         <tr>27                             <td><%#Eval("LoginName") %></td>28                             <td><%#Eval("Pwd") %></td>29                             <td><%#Eval("IsEnabled") %></td>30                         </tr>31                     </ItemTemplate>32                 </asp:Repeater>33             </table>34         <%--    <a href="Demo3.aspx?pageIndex=1">1</a>35              <a  href="Demo3.aspx?pageIndex=2">2</a>36              <a>3</a>37             <a>4</a>38              <a>5</a>--%>39             <%--调用后台PageBar类中的代码--%>40             <%=PageBar %>41         </div>42     </form>43 </body>44 </html>

 

View Code

View代码

2、表现层后台代码

Index.cshtml

bob体育平台 14bob体育平台 15

bob体育平台 16bob体育平台 17

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 using Chapter05.BLL; 9 using Chapter05.Models;10 using System.Text;11 12 namespace Chapter0513 {14     public partial class Demo3 : System.Web.UI.Page15     {16         //定义公共属性PageBar17         public string PageBar { get; set; }18         protected void Page_Load(object sender, EventArgs e)19         {20             if (!IsPostBack)21             {22                 //当前页23                 int pageIndex = 1;24                 //每页行数25                 int pageSize = 5;26                 //总页数27                 int pageCount = 0;28                 //总行数29                 int rowCount = 0;30                 //判断当前页是否为空31                 if (Request.QueryString["pageIndex"] != null)32                 {33 34                     pageIndex = Convert.ToInt32(Request.QueryString["pageIndex"].ToString;35                 }36 37                 LoginInfoBLL bll = new LoginInfoBLL();38                 List<LoginInfo> list = bll.GetPageData(pageIndex, pageSize, ref rowCount, ref pageCount);39                 //绑定数据源40                 this.Repeater1.DataSource = list;41                 this.Repeater1.DataBind();42                 //调用分页方法PageBar43                 this.PageBar = Chapter05.PageBar.CreatePageBar(pageIndex, pageCount);44             }45 46         }47 48         49 50     }51 }
 1 @{
 2     ViewBag.Title = "Index";
 3     Layout = "~/Views/Shared/_Layout.cshtml";
 4 }
 5 
 6 @model SJCRM.Helpers.Pager<SJCRM.Models.SystemUser>
 7 <div class="contrgcon">
 8     <div class="contrg_list">
 9         <div class="rgtit">
10             <form method="post" action="/@ViewBag.ControllerName/search">
11                 <dl>
12                     <dd>关键词:</dd>
13                     <dd>
14                         <input name="KeyWord" type="text" class="rgtxt2" /></dd>
15                     <dd>
16                         <input type="submit" value="查 询" class="rgbut1" /></dd>
17                 </dl>
18             </form>
19         </div>
20         <div class="rgc">
21             <dl class="rgt11">
22                 <dt>
23                     <a href="/systemuser/add">添加用户</a>
24                 </dt>
25             </dl>
26 
27             <div class="rg3">
28                 <table width="100%" border="0" cellspacing="0" cellpadding="0">
29                     <tr>
30                         <td class="rgtd2">用户编号</td>
31                         <td class="rgtd3">登录名</td>
32                         <td class="rgtd3">手机</td>
33                         <td class="rgtd3">Email</td>
34                         <td class="rgtd3">昵称</td>
35                         <td class="rgtd3">状态</td>
36                         <td class="rgtd4">操作</td>
37                     </tr>
38                 </table>
39 
40             </div>
41             <div class="rg4">
42                 @{int classnum = 1;}
43                 @foreach (var item in Model)
44                 {
45                     <table width="100%" border="0" cellspacing="0" cellpadding="0" @if (classnum % 2 == 0)
46                                                                                    {<text>class="rgbg2"</text>}>
47                         <tr>
48                             <td class="rgtd2">@item.SystemUserID</td>
49                             <td class="rgtd3">@(item.LogName)</td>
50                             <td class="rgtd3">@item.Mobile</td>
51                             <td class="rgtd3">@item.Email</td>
52                             <td class="rgtd3">@item.TrueName</td>
53                             <td class="rgtd3">@(item.State == 1 ? "有效" : "无效")</td>
54                             <td class="rgtd4"><a href="/systemuser/delete/@item.SystemUserID">删除</a>&nbsp;|&nbsp;<a href="/systemuser/edit/@item.SystemUserID">编辑</a>&nbsp; </td>
55                         </tr>
56                     </table>
57                                                                                    classnum = classnum + 1;
58                 }
59             </div>
60 
61         </div>
62         <div class="rg5">
63             @UIHelper.PagesHelper(ViewBag.ControllerName, Model.TotalPages, Model.PageIndex, Model.PageSize, Model.TotalCount, Model.HasPreviousPage, Model.HasNextPage, Model.PageList)
64         </div>
65     </div>
66 </div>
67 <script>
68     //菜单显示
69     $("#@(ViewBag.ControllerName)sys").addClass("hbg");
70     $("#@(ViewBag.ControllerName)").removeClass("navli");
71     $("#@(ViewBag.ControllerName)").addClass("navlihg");
72     $(".@(ViewBag.ControllerName)sys").each(function (index, item) {
73         $(this).show();
74     });
75 </script>

View Code

View Code

3、逻辑判断层代码

Add.cshtml

bob体育平台 18bob体育平台 19

bob体育平台 20bob体育平台 21

 1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using Chapter05.DAL; 8 using Chapter05.Models; 9 10 namespace Chapter05.BLL11 {12     public class LoginInfoBLL13     {14         public DataSet GetAll()15         {16             LoginInfoDAL dal = new LoginInfoDAL();17             return dal.GetAll();18         }19 20         public List<LoginInfo> GetAllList()21         {22             LoginInfoDAL dal = new LoginInfoDAL();23             return dal.GetAllList();24         }25 26         public bool DoDelete(string id)27         {28             LoginInfoDAL dal = new LoginInfoDAL();29             return dal.Delete > 0;30         }31 32         public LoginInfo GetEntityById(string id)33         {34             return new LoginInfoDAL().GetEntityById;35         }36 37         public bool ModifyLoginInfo(LoginInfo info)38         {39             return new LoginInfoDAL().Update > 0;40         }41         //存储过程分页42         public List<LoginInfo> GetPageData(int pageIndex, int pageSize, ref int rowCount, ref int pageCount) {43 44             return new LoginInfoDAL().GetPageData(pageIndex, pageSize, ref rowCount, ref pageCount);45         }46     }47 48 49 }
 1 @{
 2     ViewBag.Title = "Add";
 3     Layout = "~/Views/Shared/_Layout.cshtml";
 4 }
 5 <div class="contrgcon">
 6     <div class="rgtit3">
 7         <div class="tit1">
 8             <h3>添加用户</h3>
 9         </div>
10         <div class="rgtit3con">
11             @using (Html.BeginForm())
12             {
13                 <dl>
14                     <dd class="dc1">用户名:</dd>
15                     <dd>
16                         <input name="LogName" type="text" class="rgtxt2" datatype="*" nullmsg="请输入产品名称" />
17                     </dd>
18                 </dl>
19               
20                 <dl>
21                     <dd class="dc1">昵称:</dd>
22                     <dd>
23                         <input name="TrueName" type="text" class="rgtxt2" datatype="n" nullmsg="请输入产品名称" />
24                     </dd>
25                 </dl>
26                 
27                 <dl>
28                     <dd class="dc1">Email:</dd>
29                     <dd>
30                         <input type="text" name="Email" id="Email" class="rgtxt2" />
31                     </dd>
32                 </dl>
33                 <dl>
34                     <dd class="dc1">手机号:</dd>
35                     <dd>
36                         <input type="text" name="Mobile" id="Mobile" class="rgtxt2" />
37                     </dd>
38                 </dl>
39                 <dl>
40                     <dd class="dc1">密码:</dd>
41                     <dd>
42                         <input type="text" name="Password" id="Password" class="rgtxt2" />
43                     </dd>
44                 </dl>
45                 <dl>
46                     <dd class="dc1">确认密码:</dd>
47                     <dd>
48                         <input type="text" name="PassWordto" id="PassWordto" class="rgtxt2" />
49                     </dd>
50                 </dl>
51                 <dl>
52                     <dd class="dc1">岗位:</dd>
53                     <dd>
54                         @Html.DropDownList("JobID", ViewBag.JobID as SelectList)
55                     </dd>
56                 </dl>
57                 <dl>
58                     <dd class="dc1">部门:</dd>
59                     <dd>
60                          @Html.DropDownList("DepartmentID", ViewBag.DepartmentID as SelectList)
61                     </dd>
62                 </dl>
63                 <dl>
64                     <dd class="dc1">角色:</dd>
65                     <dd>
66                          @Html.DropDownList("RoleID", ViewBag.RoleID as SelectList)
67                     </dd>
68                 </dl>
69                 
70                 <dl>
71                     <dd>
72                         <input type="submit" value="提 交" class="rgbut4" />
73                     </dd>
74                 </dl>
75             }
76         </div>
77     </div>
78 </div>
79 <script>
80     //菜单显示
81     $("#@(ViewBag.ControllerName)sys").addClass("hbg");
82     $("#@(ViewBag.ControllerName)").removeClass("navli");
83     $("#@(ViewBag.ControllerName)").addClass("navlihg");
84     $(".@(ViewBag.ControllerName)sys").each(function (index, item) {
85         $(this).show();
86     });
87 </script>

View Code

View Code

4、数据访问层代码

Edit.cshtml

 ——LoginInfoDAL

bob体育平台 22bob体育平台 23

bob体育平台 24bob体育平台 25

 1 @{
 2     ViewBag.Title = "Add";
 3     Layout = "~/Views/Shared/_Layout.cshtml";
 4 }
 5 @model SJCRM.Models.SystemUser
 6 <div class="contrgcon">
 7     <div class="rgtit3">
 8         <div class="tit1">
 9             <h3>编辑用户</h3>
10         </div>
11         <div class="rgtit3con">
12             @using (Html.BeginForm())
13             {
14                 <dl>
15                     <dd class="dc1">用户名:</dd>
16                     <dd>
17                         <input name="LogName" type="text" class="rgtxt2" datatype="*" nullmsg="请输入产品名称" value="@Model.LogName"/>
18                     </dd>
19                 </dl>
20               
21                 <dl>
22                     <dd class="dc1">昵称:</dd>
23                     <dd>
24                         <input name="TrueName" type="text" class="rgtxt2" datatype="n" nullmsg="请输入产品名称" value="@Model.TrueName" />
25                     </dd>
26                 </dl>
27                 
28                 <dl>
29                     <dd class="dc1">Email:</dd>
30                     <dd>
31                         <input type="text" name="Email" id="Email" class="rgtxt2" value="@Model.Email" />
32                     </dd>
33                 </dl>
34                 <dl>
35                     <dd class="dc1">手机号:</dd>
36                     <dd>
37                         <input type="text" name="Mobile" id="Mobile" class="rgtxt2" value="@Model.Mobile" />
38                     </dd>
39                 </dl>
40                 <dl>
41                     <dd class="dc1">修改密码:</dd>
42                     <dd>
43                         <input type="text" name="Password" id="Password" class="rgtxt2" value="@Model.Password" />
44                     </dd>
45                 </dl>
46                 
47                 <dl>
48                     <dd class="dc1">岗位:</dd>
49                     <dd>
50                         @Html.DropDownList("JobID", ViewBag.JobID as SelectList)
51                     </dd>
52                 </dl>
53                 <dl>
54                     <dd class="dc1">部门:</dd>
55                     <dd>
56                          @Html.DropDownList("DepartmentID", ViewBag.DepartmentID as SelectList)
57                     </dd>
58                 </dl>
59                 <dl>
60                     <dd class="dc1">角色:</dd>
61                     <dd>
62                          @Html.DropDownList("RoleID", ViewBag.RoleID as SelectList)
63                     </dd>
64                 </dl>
65                 
66                 <dl>
67                     <dd>
68                         <input type="submit" value="提 交" class="rgbut4" />
69                     </dd>
70                 </dl>
71             }
72         </div>
73     </div>
74 </div>
75 <script>
76     //菜单显示
77     $("#@(ViewBag.ControllerName)sys").addClass("hbg");
78     $("#@(ViewBag.ControllerName)").removeClass("navli");
79     $("#@(ViewBag.ControllerName)").addClass("navlihg");
80     $(".@(ViewBag.ControllerName)sys").each(function (index, item) {
81         $(this).show();
82     });
83 </script>
  1 using System;  2 using System.Collections.Generic;  3 using System.Data;  4 using System.Linq;  5 using System.Text;  6 using System.Threading.Tasks;  7 using Chapter05.Models;  8 using System.Data.SqlClient;  9  10 namespace Chapter05.DAL 11 { 12     public class LoginInfoDAL 13     { 14  15         #region 获取全部数据 16  17         /// <summary> 18         ///  19         /// </summary> 20         /// <returns></returns> 21         public DataSet GetAll() 22         { 23             //1.定义SQL 24             string sql = "select * from LoginInfo"; 25             return SqlHelper.GetDataSet(sql, CommandType.Text); 26         } 27  28         public List<LoginInfo> GetAllList() 29         { 30             List<LoginInfo> result = new List<LoginInfo>(); 31             DataSet ds = new DataSet(); 32             ds = GetAll(); 33             if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 34             { 35                 foreach (DataRow dr in ds.Tables[0].Rows) 36                 { 37                     LoginInfo temp = DataRowToEntity; 38                     // 转换的结果 ,添加到 集合中 39                     result.Add; 40                 } 41             } 42             else 43             { 44                 result = null; 45             } 46             return result; 47         } 48  49  50         #endregion 51  52         #region 查询单个实体 53  54         public LoginInfo GetEntityById(string id) 55         { 56             LoginInfo result = null; 57             string sql = "select * from LoginInfo where id=@id"; 58             SqlParameter param = new SqlParameter("@id", SqlDbType.VarChar, 50); 59             param.Value = id; 60  61             DataSet ds = SqlHelper.GetDataSet(sql, CommandType.Text, param); 62             if (ds.Tables[0].Rows.Count > 0) 63             { 64                 result = DataRowToEntity(ds.Tables[0].Rows[0]); 65             } 66             return result; 67         } 68  69         #endregion 70  71         #region 分页数据 72         //定义集合类型的方法,并传入参数,声明输出参数 73         public List<LoginInfo> GetPageData(int pageIndex, int pageSize, ref int rowCount, ref int pageCount) 74         { 75             //定义数据对象集合 76             List<LoginInfo> result = new List<LoginInfo>(); 77             //调用存储过程 78             string sql = "PROC_PageData_LoginInfo"; 79  80             SqlParameter[] parameters = new SqlParameter[] {  81             new SqlParameter("@pageIndex",SqlDbType.Int), 82             new SqlParameter("@pageSize",SqlDbType.Int), 83             new SqlParameter("@rowCount",SqlDbType.Int), 84             new SqlParameter("@pageCount",SqlDbType.Int) 85             }; 86  87             parameters[0].Value = pageIndex; 88             parameters[1].Value = pageSize; 89             //声明参数类型:输出参数 90             parameters[2].Direction = ParameterDirection.Output; 91             parameters[3].Direction = ParameterDirection.Output; 92             //调用sqlhelper方法,得到数据集合,命令类型为存储过程 93             DataSet ds = SqlHelper.GetDataSet(sql, CommandType.StoredProcedure, parameters); 94             //给输出参数赋值 95             rowCount = Convert.ToInt32(parameters[2].Value); 96             pageCount = Convert.ToInt32(parameters[3].Value); 97             //判断数据集是否为空和数据集合第一张表中是否有数据 98             if (ds != null && ds.Tables[0].Rows.Count > 0) 99             {100                 //遍历第一张表中的每一行数据101                 foreach (DataRow dr in ds.Tables[0].Rows)102                 {103                     //实例化公共模型LoginInfo104                     LoginInfo temp = new LoginInfo();105                     //调用方法将每一行数据转换为对象,并赋值给temp对象106                     temp = DataRowToEntity;107                     //将temp对象赋给对象集合result108                     result.Add;109                 }110             }111             else112             {113                 result = null;114             }115             //返回对象集合116             return result;117         }118 119         #endregion120 121         #region 把数据行转化成  对象122 123         private LoginInfo DataRowToEntity(DataRow dr)124         {125             LoginInfo temp = new LoginInfo();126             temp.ID = dr["ID"].ToString();127             temp.LoginName = dr["LoginName"].ToString();128             temp.Pwd = dr["Pwd"].ToString();129             temp.IsEnabled = dr["IsEnabled"].ToString();130             temp.IsDelete = Convert.ToInt32(dr["IsDelete"]);131             return temp;132         }133 134         #endregion135 136         #region 根据主键删除数据137         public int Delete(string id)138         {139             string sql = "delete from LoginInfo  where id=@id";140 141             SqlParameter param = new SqlParameter("@id", SqlDbType.VarChar, 50);142             param.Value = id;143             return SqlHelper.ExecuteNonquery(sql, CommandType.Text, param);144 145         }146 147 148         #endregion149 150         #region 修改数据151 152         public int Update(LoginInfo loginInfo)153         {154             string sql = "update LoginInfo set LoginName=@loginName,Pwd=@pwd,IsDelete=@isDelete where Id=@id";155 156             SqlParameter[] paramters = new SqlParameter[] { 157                 new SqlParameter("@loginName",SqlDbType.VarChar,50),158                 new SqlParameter("@pwd",SqlDbType.VarChar,16),159                 new SqlParameter("@isDelete",SqlDbType.Int),160                 new SqlParameter("@id",SqlDbType.VarChar,50)161             };162 163             paramters[0].Value = loginInfo.LoginName;164             paramters[1].Value = loginInfo.Pwd;165             paramters[2].Value = loginInfo.IsDelete;166             paramters[3].Value = loginInfo.ID;167 168             return SqlHelper.ExecuteNonquery(sql, CommandType.Text, paramters);169         }170 171         #endregion172     }173 }

View Code

View Code

Search.cshtml

 ——SQLHelper

bob体育平台 26bob体育平台 27

bob体育平台 28bob体育平台 29

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@model SJCRM.Helpers.Pager<SJCRM.Models.SystemUser>
<div class="contrgcon">
    <div class="contrg_list">
        <div class="rgtit">
            <form method="post" action="/@ViewBag.ControllerName/search">
                <dl>
                    <dd>关键词:</dd>
                    <dd>
                        <input name="KeyWord" type="text" class="rgtxt2" /></dd>
                    <dd>
                        <input type="submit" value="查 询" class="rgbut1" /></dd>
                </dl>
            </form>
        </div>
        <div class="rgc">
            <dl class="rgt11">
                <dt>
                    <a href="/systemuser/add">添加用户</a>
                </dt>
            </dl>

            <div class="rg3">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td class="rgtd2">用户编号</td>
                        <td class="rgtd3">登录名</td>
                        <td class="rgtd3">手机</td>
                        <td class="rgtd3">Email</td>
                        <td class="rgtd3">昵称</td>
                        <td class="rgtd3">状态</td>
                        <td class="rgtd4">操作</td>
                    </tr>
                </table>

            </div>
            <div class="rg4">
                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" @if (classnum % 2 == 0)
                                                                                   {<text>class="rgbg2"</text>}>
                        <tr>
                            <td class="rgtd2">@item.SystemUserID</td>
                            <td class="rgtd3">@(item.LogName)</td>
                            <td class="rgtd3">@item.Mobile</td>
                            <td class="rgtd3">@item.Email</td>
                            <td class="rgtd3">@item.TrueName</td>
                            <td class="rgtd3">@(item.State == 1 ? "有效" : "无效")</td>
                            <td class="rgtd4"><a href="/systemuser/delete/@item.SystemUserID">删除</a>&nbsp;|&nbsp;<a href="/systemuser/edit/@item.SystemUserID">编辑</a>&nbsp; </td>
                        </tr>
                    </table>
                                                                                   classnum = classnum + 1;
                }
            </div>

        </div>

    </div>
</div>
<script>
    //菜单显示
    $("#@(ViewBag.ControllerName)sys").addClass("hbg");
    $("#@(ViewBag.ControllerName)").removeClass("navli");
    $("#@(ViewBag.ControllerName)").addClass("navlihg");
    $(".@(ViewBag.ControllerName)sys").each(function (index, item) {
        $(this).show();
    });
</script>
  1 using System;  2 using System.Collections.Generic;  3 using System.Configuration;  4 using System.Data;  5 using System.Data.SqlClient;  6 using System.Linq;  7 using System.Text;  8 using System.Threading.Tasks;  9  10 namespace Chapter05.DAL 11 { 12     public class SqlHelper 13     { 14         /// <summary> 15         /// 获取链接字符串 16         /// </summary> 17         private static readonly string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; 18  19         /// <summary> 20         /// 执行查询操作  21         /// </summary> 22         /// <param name="sql">SQL 语句</param> 23         /// <param name="type">CommandType 命令的类型【text=>sql 脚本,StoredProcedure=>存储过程】</param> 24         /// <param name="pars">参数</param> 25         /// <returns>返回结果集 dataset</returns> 26         public static DataSet GetDataSet(string sql, CommandType type, params SqlParameter[] pars) 27         { 28             //1.创建链接 29             SqlConnection conn = new SqlConnection; 30             //2.打开链接 31             conn.Open(); 32             //3.创建命令对象 33             SqlCommand cmd = new SqlCommand(sql, conn); 34             //4.创建 适配器 35             SqlDataAdapter da = new SqlDataAdapter; 36             // 当传入的参数不为空时,直接添加到Cmd 对象的Parameters 属性上 37             if (pars != null) 38             { 39                 foreach (var item in pars) 40                 { 41                     item.Value = item.Value == null ? DBNull.Value : item.Value; 42                     cmd.Parameters.Add; 43                 } 44                 //cmd.Parameters.AddRange; 45             } 46             //根据 使用者 传入的命令类型,给 Cmd 对象的CommandType 属性赋值; 47             cmd.CommandType = type; 48             DataSet ds = new DataSet(); 49             da.Fill; 50             //关闭链接 51             conn.Close(); 52             return ds; 53         } 54  55  56         /// <summary> 57         /// 执行SQL语句:返回 影响行数 58         /// </summary> 59         /// <param name="sql">SQL 语句</param> 60         /// <param name="type">CommandType 命令的类型【text=>sql 脚本,StoredProcedure=>存储过程】</param> 61         /// <param name="pars">参数</param> 62         /// <returns></returns> 63         public static int ExecuteNonquery(string sql, CommandType type, params SqlParameter[] pars) 64         { 65             using (SqlConnection conn = new SqlConnection 66             { 67                 using (SqlCommand cmd = new SqlCommand(sql, conn)) 68                 { 69                     if (pars != null) 70                     { 71                         foreach (var item in pars) 72                         { 73                             item.Value = item.Value == null ? DBNull.Value : item.Value; 74                             cmd.Parameters.Add; 75                         } 76                         //cmd.Parameters.AddRange; 77  78                     } 79                     cmd.CommandType = type; 80                     conn.Open(); 81                     return cmd.ExecuteNonQuery(); 82                 } 83             } 84         } 85  86         /// <summary> 87         /// 执行查询,并返回查询所返回的结果集中第一行的第一列 88         /// </summary> 89         /// <param name="sql"></param> 90         /// <param name="type"></param> 91         /// <param name="pars"></param> 92         /// <returns></returns> 93         public static object ExecuteScalar(string sql, CommandType type, params SqlParameter[] pars) 94         { 95             using (SqlConnection conn = new SqlConnection 96             { 97                 using (SqlCommand cmd = new SqlCommand(sql, conn)) 98                 { 99                     if (pars != null)100                     {101                         //cmd.Parameters.AddRange;102                         foreach (var item in pars)103                         {104                             item.Value = item.Value == null ? DBNull.Value : item.Value;105                             cmd.Parameters.Add;106                         }107                     }108                     cmd.CommandType = type;109                     conn.Open();110                     return cmd.ExecuteScalar();111                 }112             }113         }114     }115 }

View Code

View Code

 角色管理

5、封装的实体类--PageBar代码(实现点击数字切换分页页面)

bob体育平台 30

bob体育平台 31bob体育平台 32

Controller代码

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web; 6  7 namespace Chapter05 8 { 9     public class PageBar10     {11         /// <summary>12         /// 分页方法13         /// </summary>14         /// <param name="pageIndex"></param>15         /// <param name="pageCount"></param>16         /// <returns></returns>17         public static string CreatePageBar(int pageIndex, int pageCount)18         {19             //计算起始页码20             int startPage = pageIndex - 5 < 1 ? 1 : pageIndex - 5;21             // 计算结束页码22             int endPage = startPage + 9 > pageCount ? pageCount : startPage + 9;23 24             StringBuilder sb = new StringBuilder();25             // 设置Pagebar 的容器26             sb.Append("<div class='pager'>");27             // 判断当前页码是否是第一页28             if (pageIndex != 1)29             {30                 //首页31                 sb.AppendFormat("<a href='Demo3.aspx?pageIndex={0}'>{1}</a>", 1, "首页");32                 //上一页33                 sb.AppendFormat("<a href='Demo3.aspx?pageIndex={0}'>{1}</a>", pageIndex - 1, "上一页");34             }35             // 循环生成 页码标签36             for (int i = startPage; i <= endPage; i++)37             {38                 //判断 如果页码等于当前页, 设置Current 样式39                 if (i == pageIndex)40                 {41                     sb.AppendFormat("<a class='current' >{0}</a>", i);42                 }43                 else44                 {45                     sb.AppendFormat("<a href='Demo3.aspx?pageIndex={0}'>{0}</a>", i);46                 }47             }48             //判断 当前页,是否是 尾页49             if (pageIndex != endPage)50             {51                 // 下一页52                 sb.AppendFormat("<a href='Demo3.aspx?pageIndex={0}'>{1}</a>", pageIndex + 1, "下一页");53                 //尾页54                 sb.AppendFormat("<a href='Demo3.aspx?pageIndex={0}'>{1}</a>", pageCount, "尾页");55             }56             sb.Append("</div>");57             return sb.ToString();58         }59     }60 }

bob体育平台 33bob体育平台 34

View Code

  1  public class RoleController : Controller
  2     {
  3         RoleRepository roleryrepository = new RoleRepository();
  4         MenuRepository menurepository = new MenuRepository();
  5         RoleMenuMappingRepository rmmrepositoy = new RoleMenuMappingRepository();
  6 
  7         #region 列表
  8         /// <summary>
  9         /// 默认页面
 10         /// </summary>
 11         /// <returns></returns>
 12         /// 
 13         [Authorize]
 14         public ActionResult Index(int? page)
 15         {
 16             ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
 17             var source = roleryrepository.GetModelList();
 18             return View(roleryrepository.GetPageModelList(source, 10, page ?? 1));
 19         }
 20         #endregion
 21 
 22         #region 添加
 23         /// <summary>
 24         /// 添加页
 25         /// </summary>
 26         /// <param name="model">实体类</param>
 27         /// <returns></returns>
 28         [Authorize]
 29         public ActionResult Add(Role model)
 30         {
 31             ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
 32             var menu = menurepository.GetModelListByState(1);
 33 
 34             ViewBag.Menu = new SelectList(menu, "MenuID", "MenuName");
 35             // ViewData["State"] = SelectListHelper.GetIsOrNoSelectList(0);
 36             return View();
 37         }
 38         public string Log()
 39         {
 40             string meg = "";
 41             //int user = int.Parse(CookieHelper.GetValue("SysUserID"));
 42             //string ip = IPHelper.GetUserIp;
 43             foreach (var item in RouteData.Values)
 44             {
 45                 meg += item.Key.ToUpper() + ":" + item.Value + "|";
 46             }
 47             return meg;
 48         }
 49         /// <summary>
 50         /// 添加事件
 51         /// </summary>
 52         /// <param name="model">实体类</param>
 53         /// <param name="fc"></param>
 54         /// <returns></returns>
 55         [Authorize, HttpPost, ValidateInput(false)]
 56         public ActionResult Add(Role model, FormCollection fc, int[] Menu)
 57         {
 58             try
 59             {
 60 
 61 
 62                 ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
 63                 model.State = 1;
 64 
 65                 model.CreateTime = DateTime.Now;
 66                 roleryrepository.SaveOrEditModel(model);
 67                 if (Menu.Length > 0)
 68                 {
 69 
 70                     foreach (int gsi in Menu)
 71                     {
 72                         RoleMenuMapping rmm = new RoleMenuMapping();
 73                         rmm.MenuID = gsi;
 74                         rmm.RoleID = model.RoleID;
 75                         rmmrepositoy.SaveOrEditModel(rmm);
 76                     }
 77                 }
 78                 else
 79                 {
 80                     return RedirectToAction("Index");
 81                 }
 82                
 83             }
 84             catch (Exception ex)
 85             {
 86 
 87                 Log4netHelper.InvokeErrorLog(MethodBase.GetCurrentMethod().DeclaringType, ViewBag.ControllerName + "添加方法出错-Author:ryanding IP地址[" + IPHelper.GetUserIp + "]" + "操作人ID:" + CookieHelper.GetValue("SystemUserID") + "当前来源信息:" + Log(), ex);
 88             }
 89             return View("404");
 90             
 91         }
 92         #endregion
 93 
 94         #region 删除
 95         /// <summary>
 96         /// 删除
 97         /// </summary>
 98         /// <param name="id">删除编号</param>
 99         /// <returns></returns>
100         [Authorize]
101         public ActionResult Delete(int id)
102         {
103             ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
104             var model = roleryrepository.GetModel(id);
105             model.State = 0;
106             if (model != null)
107             {
108                 roleryrepository.SaveOrEditModel(model);
109                 return RedirectToAction("index");
110             }
111             else
112                 return View("404");
113           
114 
115         }
116         #endregion
117 
118         #region 查询
119         [Authorize]
120         public ActionResult Search(int? page)
121         {
122             ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
123             string name = "";
124             if (page == null)
125             {
126                 name = Request.Form["KeyWord"];
127                 CookieHelper.SetObj("KeyWord", 0, name);
128             }
129             else
130                 name = CookieHelper.GetValue("KeyWord");
131 
132             var query = QueryBuilder.Create<Role>().Like(d => d.RoleName, name);
133             var source = roleryrepository.GetModelList().Where(query.Expression);
134             return View(new Pager<Role>(source, page ?? 1, 10));
135         }
136         #endregion
137 
138         #region 编辑
139         [Authorize]
140         public ActionResult Edit(int id)
141         {
142             ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
143             var rmmList = rmmrepositoy.GetModelList().Where(d => d.RoleID == id);
144 
145 
146             var menu = menurepository.GetModelListByState(1);
147             ViewBag.Menu = new SelectList(menu, "MenuID", "MenuName", string.Join(",", rmmList.Select(p => p.MenuID)));
148             var model = roleryrepository.GetModel(id);
149             if (model != null)
150             {
151                 ViewData["State"] = SelectListHelper.GetStateSelectList(model.State);
152                 return View(model);
153             }
154             else
155                 return View("404");
156         }
157 
158         [Authorize, AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
159         public ActionResult Edit(int id, FormCollection fc, int[] Menu)
160         {
161             ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
162             var model = roleryrepository.GetModel(id);
163 
164             if (model != null)
165             {
166                 if (Menu.Length > 0)
167                 {
168                     rmmrepositoy.DeleteModel(rmmrepositoy.GetModelList().Where(d => d.RoleID == model.RoleID));
169                     foreach (int gsi in Menu)
170                     {
171                         RoleMenuMapping rmm = new RoleMenuMapping();
172                         rmm.MenuID = gsi;
173                         rmm.RoleID = model.RoleID;
174                         rmmrepositoy.SaveOrEditModel(rmm);
175                     }
176                 }
177 
178 
179                 UpdateModel(model);
180                 roleryrepository.SaveOrEditModel(model);
181                 return RedirectToAction("index");
182             }
183             else
184                 return View("404");
185         }
186         #endregion
187     }

6、公共类--Model层代码

View Code

    ——封装公共属性

View代码

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Chapter05.Models 8 { 9     public class LoginInfo10     {11         public string ID { get; set; }12         public string LoginName { get; set; }13         public string Pwd { get; set; }14         public string IsEnabled { get; set; }15         public int IsDelete { get; set; }16     }17 }

Index.cshtml

7、页码条--PageBar的样式表中CSS样式代码

bob体育平台 35bob体育平台 36

bob体育平台 37bob体育平台 38

 1 @{
 2     ViewBag.Title = "Index";
 3     Layout = "~/Views/Shared/_Layout.cshtml";
 4 }
 5 
 6 @model SJCRM.Helpers.Pager<SJCRM.Models.Role>
 7 <div class="contrgcon">
 8     <div class="contrg_list">
 9         <div class="rgtit">
10             <form method="post" action="/@ViewBag.ControllerName/search">
11                 <dl>
12                     <dd>关键词:</dd>
13                     <dd>
14                         <input name="KeyWord" type="text" class="rgtxt2" /></dd>
15                     <dd>
16                         <input type="submit" value="查 询" class="rgbut1" /></dd>
17                 </dl>
18             </form>
19         </div>
20         <div class="rgc">
21             <dl class="rgt11">
22                 <dt>
23                    <a href="/@ViewBag.ControllerName/add">添加</a>
24                 </dt>
25             </dl>
26 
27             <div class="rg3">
28                 <table width="100%" border="0" cellspacing="0" cellpadding="0">
29                     <tr>
30                         <td class="rgtd2">编号</td>
31                         <td class="rgtd2">角色名称</td>
32                         <td class="rgtd3">角色内容</td>
33                         <td class="rgtd3">创建时间</td>
34                         <td class="rgtd3">状态</td>
35                         <td class="rgtd4">操作</td>
36                     </tr>
37                 </table>
38 
39             </div>
40             <div class="rg4">
41                 @{int classnum = 1;}
42                 @foreach (var item in Model)
43                 {
44                     <table width="100%" border="0" cellspacing="0" cellpadding="0" @if (classnum % 2 == 0)
45                                                                                    {<text>class="rgbg2"</text>}>
46                         <tr>
47                             <td class="rgtd2">@item.RoleID</td>
48                             <td class="rgtd2">@item.RoleName</td>
49                             <td class="rgtd3">@item.RoleContent</td>
50                             <td class="rgtd3">@String.Format("{0:yyyy-MM-dd}", item.CreateTime)</td>
51                             <td class="rgtd3">@(item.State == 1 ? "有效" : "无效")</td>
52                             <td class="rgtd4"><a href="/@ViewBag.ControllerName/delete/@item.RoleID">删除</a>&nbsp;|&nbsp;<a href="/@ViewBag.ControllerName/edit/@item.RoleID">编辑</a>&nbsp; </td>
53                         </tr>
54                     </table>
55                                                                                    classnum = classnum + 1;
56                 }
57             </div>
58 
59         </div>
60         <div class="rg5">
61            @UIHelper.PagesHelper(ViewBag.ControllerName, Model.TotalPages, Model.PageIndex, Model.PageSize, Model.TotalCount, Model.HasPreviousPage, Model.HasNextPage, Model.PageList);
62  
63         </div>
64     </div>
65 </div>
66 <script>
67     //菜单显示
68     $("#systemusersys").addClass("hbg");
69     $("#@(ViewBag.ControllerName)").removeClass("navli");
70     $("#@(ViewBag.ControllerName)").addClass("navlihg");
71     $(".systemusersys").each(function (index, item) {
72         $(this).show();
73     });
74 </script>
 1 /*PageBarCSS*/ 2  3 .pager { 4     font-size: 12px; 5     margin: 25px 0; 6     text-align: center; 7     color: #2e6ab1; 8     line-height: 200%; 9     overflow: hidden;10 }11 12     .pager a {13         border: 1px solid #9aafe5;14         color: #2e6ab1;15         margin: 0 2px;16         padding: 2px 5px;17         text-decoration: none;18     }19 20     .pager span.current {21         background-color: #2e6ab1;22         border: 1px solid navy;23         color: #fff;24         font-weight: bold;25         margin: 0 2px;26         padding: 2px 5px;27     }28 29     .pager a.current {30         background-color: #2e6ab1;31         border: 1px solid navy;32         color: #fff;33         font-weight: bold;34         margin: 0 2px;35         padding: 2px 5px;36     }

View Code

View Code

Add.cshtml

8、SQL数据库中的分页存储过程

bob体育平台 39bob体育平台 40

bob体育平台 41bob体育平台 42

 1 @using SJCRM.Helpers
 2 @{
 3     ViewBag.Title = "Add";
 4     Layout = "~/Views/Shared/_Layout.cshtml";
 5 }
 6 <div class="contrgcon">
 7     <div class="rgtit3">
 8         <div class="tit1">
 9             <h3>添加</h3>
10         </div>
11         <div class="rgtit3con">
12             @using (Html.BeginForm())
13             {
14                 <dl>
15                     <dd class="dc1">角色名称:</dd>
16                     <dd>
17                         <input name="RoleName" type="text" class="rgtxt2" datatype="*" nullmsg="请输入名称" />
18                     </dd>
19                 </dl>
20               
21       <dl>
22                     <dd class="dc1">角色权限:</dd>
23                     <dd>
24                         @Html.CheckBoxList("Menu")
25                     </dd>
26                 </dl>
27                
28                 <dl>
29                     <dd class="dc1">角色内容:</dd>
30                     <dd>
31                          <textarea rows="10" cols="82" name="RoleContent" id="RoleContent"></textarea>
32                     </dd>
33                 </dl>
34                 <dl>
35                     <dd>
36                         <input type="submit" value="提 交" class="rgbut4" />
37                     </dd>
38                 </dl>
39             }
40         </div>
41     </div>
42 </div>
43 <script>
44     //菜单显示
45     $("#systemusersys").addClass("hbg");
46     $("#@(ViewBag.ControllerName)").removeClass("navli");
47     $("#@(ViewBag.ControllerName)").addClass("navlihg");
48     $(".systemusersys").each(function (index, item) {
49         $(this).show();
50     });
51 </script>
 1 use empdb 2 go 3 create proc proc_pagedate_empinfor 4 @pageIndex int, 5 --声明为float类型的目的:使select @pageCount=CEILING(@rowCount/@pageSize)可以向上取整 6 @pageSize float, 7 @rowCount int out, 8 @pageCount int out 9 as10     begin11     select * from (select ROW_NUMBER() over (order by empId) as Num,* from empinfo) as temp 12     where Num>(@pageIndex-1)*@pageSize and Num<=@pageIndex*@pageSize13     select @rowCount=COUNT(1)  from empinfo14     select @pageCount=CEILING(@rowCount/@pageSize)15     end16 go17 declare @pageCount int,18         @rowCount int19 --1:@pageIndex;3:@pageSize20 exec proc_pagedate_empinfor 1,3,@rowCount out,@pageCount out21 select @rowCount as 总条数,@pageCount as 总页数

版权声明:本文由bob体育app发布于编程,转载请注明出处:bob体育app主攻ASP.NET.4.5.1 MVC5.0之重生:系统角色与权限(二)