搜索: 标题内容作者  
  首页C#教程C#技巧
背景:
阅读新闻

C# WinForm中实现基于角色的权限菜单

[日期:2008-12-15]   来源:互联网整理  作者:佚名   [字体: ]
    新闻简介: 基于角色的权限菜单功能的实现在有开发经验的程序员看来仅仅是小菜一碟,然而却让许多初学者苦不堪言。为此,我将我近期花了几天时间写的权限菜单写成文字贴上博客给初学者参考。由于自己也是一个正在努力学习的菜鸟,对问题的分析和见解必然不够透彻,还望过路的老师们多多批评为谢!
        关 键 词:  

//将数据集转换成实体集合
foreach (DataRow row in dsRightsRelation.Tables["RightsRelation"].Rows)
{
Model.RightsRelation tmpRightsRelation = new Model.RightsRelation();
tmpRightsRelation.Id = Convert.ToInt32(row["Id"]);
tmpRightsRelation.OperatorId = Convert.ToInt32(row["OperatorId"]);
tmpRightsRelation.OperatorName = Convert.ToString(row["OperatorName"]);
tmpRightsRelation.RightsGroupId = Convert.ToInt32(row["RightsGroupId"]);
tmpRightsRelation.RightsGroupName = Convert.ToString(row["RightsGroupName"]);

rightsRelationList.Add(tmpRightsRelation);
}

//返回所有客户集合
return rightsRelationList;
}

/// <summary>
/// 根据操作员 ID 获取对应的所有权限关系
/// </summary>
/// <param name="id">操作员 ID</param>
/// <returns>权限关系集合</returns>
public List<Model.RightsRelation> GetRightsRelationByOperatorId(int id)
{
//创建数据集
DataSet dsRightsRelation = new DataSet("RightsRelation");
//创建客户集合
List<Model.RightsRelation> rightsRelationList = new List<Model.RightsRelation>();
//创建查询客户信息的 SQL
string sqlTxt = string.Format("Select R.Id, R.OperatorId, O.OperatorName, R.RightsGroupId, " +
"G.GroupName As [RightsGroupName] From RightsRelation As R Join Operator As O " +
"On R.OperatorId = O.Id Join RightsGroup As G On R.RightsGroupId = G.Id " +
"Where OperatorId = {0}", id);
//创建SQL执行对象
DBUtility.AbstractDBProvider dbProvider = DBUtility.AbstractDBProvider.Instance();
//执行查询操作
dsRightsRelation = dbProvider.RunCommand(sqlTxt, "RightsRelation");
//将数据集转换成实体集合
foreach (DataRow row in dsRightsRelation.Tables["RightsRelation"].Rows)
{
Model.RightsRelation tmpRightsRelation = new Model.RightsRelation();
tmpRightsRelation.Id = Convert.ToInt32(row["Id"]);
tmpRightsRelation.OperatorId = Convert.ToInt32(row["OperatorId"]);
tmpRightsRelation.OperatorName = Convert.ToString(row["OperatorName"]);
tmpRightsRelation.RightsGroupId = Convert.ToInt32(row["RightsGroupId"]);
tmpRightsRelation.RightsGroupName = Convert.ToString(row["RightsGroupName"]);

rightsRelationList.Add(tmpRightsRelation);
}

//返回所有客户集合
return rightsRelationList;
}

/// <summary>
/// 根据权限组 ID 获取与此权限组相关的权限关系数量
/// </summary>
/// <param name="id">权限组 ID</param>
/// <returns>权限关系数量</returns>
public int GetRightsRelationCountByRightsGroupId(int id)
{
// SQL命令
string sqlTxt = string.Format("Select Count(*) From RightsRelation Where RightsGroupId = {0}", id);

// 创建SQL执行对象
DBUtility.AbstractDBProvider dbProvider = DBUtility.AbstractDBProvider.Instance();
// 执行查询操作
int result = Convert.ToInt32(dbProvider.RunCommand(sqlTxt));

// 返回结果
return result;
}

#endregion
}
}

(二)业务逻辑层(在本文中仅仅起到一个数据传递者的作用)

1、操作员数据访问操作类(OperatorManager)

C#源代码清单

using System;
using System.Collections.Generic;
using System.Text;
using DALFactory = CodingMouse.CMHotelManager.DALFactory;
using IBLL = CodingMouse.CMHotelManager.IBLL;
using IDAL = CodingMouse.CMHotelManager.IDAL;
using Model = CodingMouse.CMHotelManager.Model;

namespace CodingMouse.CMHotelManager.BLL
{
/// <summary>
/// 操作员数据访问操作类
/// </summary>
public class OperatorManager : IBLL.IOperatorManager
{

#region IOperatorManager 成员

/// <summary>
/// 根据操作员名称和密码获取操作员实体
/// </summary>
/// <param name="name">操作员名称</param>
/// <param name="pwd">操作员密码</param>
/// <returns>操作员实体</returns>
public Model.Operator GetOperatorInfoByName(string name, string pwd)
{
// 超级后门管理员账户(测试用,千万别用于商业用途,要不然被抓去坐监可别怪我!)
if (name == "CodingMouse" && pwd == "20071117")
{
Model.Operator adminOperator = new Model.Operator();
adminOperator.Id = 0;
adminOperator.ModelName = name;
adminOperator.Password = pwd;
adminOperator.RightsCollection = new Dictionary<string, Model.Rights>();
adminOperator.State = true;

return adminOperator;
}

//定义并实例化抽象工厂类
DALFactory.AbstractDALFactory absDALFactory = DALFactory.AbstractDALFactory.Instance();
//调用工厂方法生成实例

If you believe an article violates your rights or the rights of others, please contact us.

收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻      
友情链接
本文评论   [发表评论] 全部评论 (0)
赞助商广告
热门评论