using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XQH.ExtJs.LinqDatabase
{
public class RoleLinqAccess
{
LinqDatabaseDataContext db = new LinqDatabaseDataContext();
/// <summary>
/// 获取所有角色
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <returns></returns>
public List<XRole> GetAllRole(int start,int limit,out int total)
{
var q = from r in db.XRole
select r;
total = q.Count();
if (limit == 0)
{
return q.ToList();
}
else
{
return q.Skip(start).Take(limit).ToList();
}
}
/// <summary>
/// 根据ID获取角色
/// </summary>
/// <param></param>
/// <returns></returns>
public XRole GetRoleById(int id)
{
var q = from r in db.XRole
where r.RoleId == id
select r;
return q.First();
}
/// <summary>
/// 新增角色
/// </summary>
/// <param></param>
/// <returns></returns>
public List<XRole> AddRole(XRole role)
{
db.XRole.InsertOnSubmit(role);
db.SubmitChanges();
return db.XRole.ToList();
}
/// <summary>
/// 根据ID删除角色
/// </summary>
/// <param></param>
/// <returns></returns>
public List<XRole> DelRoleById(int id)
{
var q = from r in db.XRole
where r.RoleId == id
select r;
db.XRole.DeleteAllOnSubmit(q);
db.SubmitChanges();
return db.XRole.ToList();
}
/// <summary>
/// 更新角色
/// </summary>
/// <param></param>
/// <returns></returns>
public List<XRole> UpdateRole(XRole role)
{
var q = from r in db.XRole
where r.RoleId == role.RoleId
select r;
foreach (XRole r in q)
{
r.RoleId = role.RoleId;
r.RoleName = role.RoleName;
}
db.SubmitChanges();
return db.XRole.ToList();
}
}
}
4.ashx代码
复制代码 代码如下:
/// <summary>
/// 获取全部角色
/// </summary>
public void GetAllRoles()
{
StringBuilder jsonData = new StringBuilder();
int start = Convert.ToInt32(Request["start"]);
int limit = Convert.ToInt32(Request["limit"]);
int total = 0;
List<XRole> lsRole = roleAccess.GetAllRole(start, limit, out total);
JsonConvert<XRole> json = new JsonConvert<XRole>();
jsonData = json.ToGridPanel(lsRole, total);
Response.Write(jsonData);
Response.End();
}
/// <summary>
/// 根据ID获取角色
/// </summary>
/// <param></param>
/// <returns></returns>
public void GetRoleById()
{
StringBuilder jsonData = new StringBuilder();
bool success = false;
string rspText = string.Empty;
string id = Request["RoleId"].ToString();
try
{
XRole role = roleAccess.GetRoleById(Convert.ToInt32(id));
success = true;
rspText = "success";
JsonConvert<XRole> json = new JsonConvert<XRole>();
jsonData = json.ToFormPanel(success, rspText, role);
}
catch (Exception ex)
{
success = false;
rspText = ex.Message;
}
Response.Write(jsonData);
Response.End();
}
/// <summary>
/// 新增角色
/// </summary>
public void AddRole()
{
string jsonStr = string.Empty;
bool success = false;
string rspText = string.Empty;
string roleName = Request["RoleName"].ToString();
XRole role = new XRole();
role.RoleName = roleName;
try
{
roleAccess.AddRole(role);
success = true;
rspText = "新增成功!";
}
catch (Exception ex)
{
success = false;
rspText = ex.Message;
}
jsonStr = "{success:" + success.ToString().ToLower() + ",message:'" + rspText + "!'}";
Response.Write(jsonStr);
Response.End();
}
/// <summary>
/// 根据角色编号修改角色
/// </summary>
public void UpdateRoleById()
{
string jsonStr = string.Empty;
bool success = false;
string rspText = string.Empty;
string RoleId = Request["RoleId"].ToString();
string RoleName = Request["RoleName"].ToString();
XRole role = new XRole();
role.RoleId = Convert.ToInt32(RoleId);
role.RoleName = RoleName;
try
{
roleAccess.UpdateRole(role);
success = true;
rspText = "修改成功!";
}
catch (Exception ex)
{
success = false;
rspText = ex.Message;
}
jsonStr = "{success:" + success.ToString().ToLower() + ",message:'" + rspText + "!'}";
Response.Write(jsonStr);
Response.End();
}
/// <summary>
/// 根据ID删除用户
/// </summary>
public void DeleteRoleById()
{
string jsonStr = string.Empty;
bool success = false;
string rspText = string.Empty;
try
{
int id = Convert.ToInt32(Request["Id"].ToString());
List<XRole> lsRole = roleAccess.DelRoleById(id);
success = true;
rspText = "success";
}
catch (Exception ex)
{
success = true;
rspText = ex.Message;
}
jsonStr = "{success:" + success.ToString().ToLower() + ",message:'" + rspText + "!'}";
Response.Write(jsonStr);
Response.End();
}
您可能感兴趣的文章: