WinForm实现对权限菜单进行管理
一、界面:

二、数据库访问类:
public class DataClass
{
private readonly string connect = ConfigurationManager.AppSettings["connectString"];
public DataClass() { } /// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public DataSet Query(string SQLString)
{ using (MySqlConnection connection = new MySqlConnection(connect))
{
DataSet ds = new DataSet();
try
{
connection.Open();
MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (MySqlException ex)
{
throw new Exception(ex.Message);
} return ds;
}
} /// <summary>
/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>SqlDataReader</returns>
public MySqlDataReader ExecuteReader(string strSQL)
{
using (MySqlConnection connection = new MySqlConnection(connect))
{
MySqlCommand cmd = new MySqlCommand(strSQL, connection);
try
{
connection.Open();
MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
throw e;
}
}
} /// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public object GetSingle(string SQLString)
{
using (MySqlConnection connection = new MySqlConnection(connect))
{
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
} public bool Exists(string strSql)
{
object obj = GetSingle(strSql);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = ;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == )
{
return false;
}
else
{
return true;
}
} /// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SQLString)
{
using (MySqlConnection connection = new MySqlConnection(connect))
{
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
} /// <summary>
/// 得到一个对象实体
/// </summary>
public ModelClass GetModelList(int MenuId)
{ StringBuilder strSql = new StringBuilder();
strSql.Append("select MenuId,MenuName,MenuRemark,MenuUrl,ParentMenuId,MenuIcon,MenuSort FROM SYS_Menu ");
strSql.Append(" where MenuId= " + MenuId); ModelClass model = new ModelClass();
DataSet ds = Query(strSql.ToString());
if (ds.Tables[].Rows.Count > )
{
if (ds.Tables[].Rows[]["MenuId"].ToString() != "")
{
model.MenuId = int.Parse(ds.Tables[].Rows[]["MenuId"].ToString());
}
model.MenuName = ds.Tables[].Rows[]["MenuName"].ToString();
model.MenuRemark = ds.Tables[].Rows[]["MenuRemark"].ToString();
model.MenuUrl = ds.Tables[].Rows[]["MenuUrl"].ToString();
if (ds.Tables[].Rows[]["ParentMenuId"].ToString() != "")
{
model.ParentMenuId = int.Parse(ds.Tables[].Rows[]["ParentMenuId"].ToString());
}
model.MenuIcon = ds.Tables[].Rows[]["MenuIcon"].ToString();
if (ds.Tables[].Rows[]["MenuSort"].ToString() != "")
{
model.MenuSort = int.Parse(ds.Tables[].Rows[]["MenuSort"].ToString());
}
return model;
}
else
{
return null;
}
} /// <summary>
/// 增加一条数据
/// </summary>
public int Add(ModelClass model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into SYS_Menu(");
strSql.Append("MenuName,MenuRemark,MenuUrl,ParentMenuId,MenuIcon,MenuSort)");
strSql.Append(" values (");
strSql.Append("'" + model.MenuName + "'," );
strSql.Append("'" + model.MenuRemark + "',");
strSql.Append("'" + model.MenuUrl + "',");
strSql.Append("'" + model.ParentMenuId + "',");
strSql.Append("'" + model.MenuIcon + "',");
strSql.Append("'" + model.MenuSort + "')");
object obj = ExecuteSql(strSql.ToString());
if (obj == null)
{
return -;
}
else
{
return Convert.ToInt32(obj);
}
} /// <summary>
/// 删除一条数据
/// </summary>
public void Delete(int MenuId)
{
string sql = "delete from SYS_Menu where MenuId = " + MenuId;
ExecuteSql(sql);
} /// <summary>
/// 更新一条数据
/// </summary>
public void Update(ModelClass model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update SYS_Menu set ");
strSql.Append("MenuName='" + model.MenuName + "',");
strSql.Append("MenuRemark='" + model.MenuRemark + "',");
strSql.Append("MenuUrl='" + model.MenuUrl + "',");
strSql.Append("ParentMenuId='" + model.ParentMenuId + "',");
strSql.Append("MenuIcon='" + model.MenuIcon + "',");
strSql.Append("MenuSort='" + model.MenuSort + "'");
strSql.Append(" where MenuId=" + model.MenuId); ExecuteSql(strSql.ToString());
}
}
三、窗体页面代码:
public partial class MenuManager : Form
{
DataClass help = new DataClass();
ModelClass model = new ModelClass();
public MenuManager()
{
InitializeComponent();
} /// <summary>
/// 窗体载入事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuManager_Load(object sender, EventArgs e)
{
QueryTreeView();//树型菜单
ComBoxBindInit(); //上级菜单下拉列表初始化
} /// <summary>
/// 树型菜单查看事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bntQuery_Click(object sender, EventArgs e)
{
QueryTreeView();//树型菜单
} /// <summary>
/// 树型菜单展示
/// </summary>
private void QueryTreeView()
{
BindListView(treeViewMenu);
//展开所有节点
treeViewMenu.ExpandAll();
this.treeViewMenu.Nodes[].EnsureVisible();//滚动打显示最上方
} #region 网站树形菜单
public void BindListView(TreeView treeView)
{
//清空树的所有节点
treeView.Nodes.Clear();
//创建根节点
TreeNode rootNode = new TreeNode();
rootNode.Text = "菜单列表";
rootNode.Name = ""; //展开所有节点
//添加根节点
treeView.Nodes.Add(rootNode);
// 获取所有节点信息
DataTable dt = help.Query(" select MenuId,MenuName,MenuRemark,MenuUrl,ParentMenuId,MenuIcon,MenuSort FROM SYS_Menu Where 1=1 order by ParentMenuId , MenuSort asc ").Tables[];
////创建其他节点
CreateChildNode(rootNode, dt);
} private void CreateChildNode(TreeNode parentNode, DataTable dataTable)
{
DataRow[] rowList = dataTable.Select("ParentMenuId='" + parentNode.Name + "'");
foreach (DataRow row in rowList)
{ //创建新节点
TreeNode node = new TreeNode();
//设置节点的属性
node.Text = row["MenuName"].ToString();
node.Name = row["MenuId"].ToString();
parentNode.Nodes.Add(node);
//递归调用,创建其他节点
CreateChildNode(node, dataTable);
}
}
#endregion /// <summary>
/// 上级菜单初始化
/// </summary>
private void ComBoxBindInit()
{
DataTable dt = help.Query("select MenuId,MenuName,MenuRemark,MenuUrl,ParentMenuId,MenuIcon,MenuSort FROM SYS_Menu ").Tables[]; DataRow dr = dt.NewRow();
dr["MenuName"] = "=============顶级菜单=============";
dr["MenuId"] = ;
dt.Rows.InsertAt(dr, ); comParentMenuId.DataSource = dt;
comParentMenuId.DisplayMember = "MenuName";
comParentMenuId.ValueMember = "MenuId"; } /// <summary>
/// 上级菜单重置(即查询所有菜单)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bntInit_Click(object sender, EventArgs e)
{
txtQueryMenu.Text = "";
ComBoxBindInit();
} /// <summary>
/// 模糊查询菜单信息绑定上级菜单下拉列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bntSelect_Click(object sender, EventArgs e)
{
DataTable dt = help.Query("select MenuId,MenuName,MenuRemark,MenuUrl,ParentMenuId,MenuIcon,MenuSort FROM SYS_Menu where 1=1 and MenuName like '%" + txtQueryMenu.Text.Trim() + "%' ").Tables[]; DataRow dr = dt.NewRow();
dr["MenuName"] = "===无===";
dr["MenuId"] = ;
dt.Rows.InsertAt(dr, ); comParentMenuId.DataSource = dt;
comParentMenuId.DisplayMember = "MenuName";
comParentMenuId.ValueMember = "MenuId";
} /// <summary>
/// 选择节点取值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeViewMenu_AfterSelect(object sender, TreeViewEventArgs e)
{
try
{
if (e.Node.Text != "菜单列表")
{
ComBoxBindInit();
model = help.GetModelList(Convert.ToInt32(e.Node.Name));
txtMenuId.Text = model.MenuId.ToString();
txtMenuName.Text = model.MenuName;
txtMenuUrl.Text = model.MenuUrl;
txtMenuSort.Text = model.MenuSort.ToString();
txtMenuIcon.Text = model.MenuIcon;
txtMenuRemark.Text = model.MenuRemark;
comParentMenuId.SelectedValue = model.ParentMenuId;
}
else
{
ClearText();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
} /// <summary>
/// 清空文本内容
/// </summary>
private void ClearText()
{
txtMenuId.Text = "";
txtMenuName.Text = "";
txtMenuUrl.Text = "";
txtMenuSort.Text = "";
txtMenuIcon.Text = "";
txtMenuRemark.Text = "";
} /// <summary>
/// 添加
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bntAdd_Click(object sender, EventArgs e)
{
try
{
getModel();//为实体类赋值 #region 条件满足判断
//菜单名称不能为空
if (txtMenuName.Text == "")
{
MessageBox.Show("菜单名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//同级菜单中不能有相同菜单名称
if(help.Exists("select count(1) from SYS_Menu where MenuName='" + txtMenuName.Text + "' and ParentMenuId=" + Convert.ToInt32(comParentMenuId.SelectedValue.ToString())))
{
MessageBox.Show("同级菜单中该名称已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//验证该级菜单是否有相同排序号
if (help.Exists("select count(1) from SYS_Menu where MenuSort=" + Convert.ToInt32(txtMenuSort.Text.Trim()) + " and ParentMenuId=" + Convert.ToInt32(comParentMenuId.SelectedValue.ToString())))
{
MessageBox.Show("该级菜单存在相同排序号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//排序号必填
if (txtMenuSort.Text == "")
{
MessageBox.Show("排序号不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
#endregion int result = help.Add(model);//添加
if (result >= )
{
MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
ClearText();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
} /// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bntUpdate_Click(object sender, EventArgs e)
{
try
{
getModel();//为实体类赋值 #region 条件满足判断
//菜单名称不能为空
if (txtMenuName.Text == "")
{
MessageBox.Show("菜单名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//同级菜单中不能有相同菜单名称
if (help.Exists("select count(1) from SYS_Menu where MenuId <>" + model.MenuId + " and MenuName='" + txtMenuName.Text + "' and ParentMenuId=" + Convert.ToInt32(comParentMenuId.SelectedValue.ToString())))
{
MessageBox.Show("同级菜单中该名称已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//验证该级菜单是否有相同排序号
if (help.Exists("select count(1) from SYS_Menu where MenuId<>" + model.MenuId + " and MenuSort=" + Convert.ToInt32(txtMenuSort.Text.Trim()) + " and ParentMenuId=" + Convert.ToInt32(comParentMenuId.SelectedValue.ToString())))
{
MessageBox.Show("该级菜单存在相同排序号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//排序号必填
if (txtMenuSort.Text == "")
{
MessageBox.Show("排序号不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
#endregion
if (txtMenuId.Text != "" && help.Exists("select count(1) from SYS_Menu where MenuId=" + model.MenuId))
{
help.Update(model);
MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show("数据记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
} /// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bntDelete_Click(object sender, EventArgs e)
{
try
{
if (txtMenuId.Text == "")
{
MessageBox.Show("请选择所要删除的记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
//删除前先判断是否被其他菜单引用
if (help.Exists("select count(1) from SYS_Menu where ParentMenuId=" + Convert.ToInt32(txtMenuId.Text)))
{
MessageBox.Show("该菜单被子级菜单引用,请先删除引用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (MessageBox.Show("确定要删除该记录吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
help.Delete(Convert.ToInt32(txtMenuId.Text));
MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
ClearText();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
} /// <summary>
/// 清空
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bntClear_Click(object sender, EventArgs e)
{
ClearText();
} /// <summary>
/// 排序号只能输入数字类型
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtMenuSort_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != && !Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
} /// <summary>
/// 为实体类赋值
/// </summary>
private void getModel()
{
if (txtMenuId.Text != "")
model.MenuId = Convert.ToInt32(txtMenuId.Text);
model.MenuName = txtMenuName.Text.Trim();
model.MenuUrl = txtMenuUrl.Text.Trim();
if (txtMenuSort.Text != "")
model.MenuSort = Convert.ToInt32(txtMenuSort.Text.Trim());
model.MenuIcon = txtMenuIcon.Text.Trim();
model.MenuRemark = txtMenuRemark.Text;
model.ParentMenuId = Convert.ToInt32(comParentMenuId.SelectedValue.ToString());
} /// <summary>
/// 光标处于上级菜单查询按下回车执行查询事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtQueryMenu_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == )
{
bntSelect_Click(sender, e);
}
} }
WinForm实现对权限菜单进行管理的更多相关文章
- Winform开发框架之权限管理系统功能介绍
权限管理系统的重要特性总结: 1) 高度集成的权限系统.独立模块,能快速整合使用.2) 符合权限的国际通用标准,基于RBAC(基于角色的访问控制)的角色权限控制.3) 多数据库架构支持,内置支持Sql ...
- 基于吉日嘎底层架构的Web端权限管理操作演示-菜单模块管理
按照顺序,这一篇介绍菜单模块管理,主要演示如下操作: 新增.修改.锁定.解锁.删除.撤销删除 排序 角色成员管理 用户成员管理 导出菜单模块数据 也许你会问,你在这自吹自擂,你这个BS的权限管理有啥缺 ...
- JavaEE权限管理系统的搭建(五)--------RBAC权限管理中的权限菜单的显示
上一小节实现了登录的实现,本小节实现登录后根据用户名查询当前用户的角色所关联的所有权限,然后进行菜单的显示.登录成功后,如下图所示,管理设置是一级菜单,管理员列表,角色管理,权限管理是二级菜单. 先来 ...
- RDIFramework.NET ━ 9.6 模块(菜单)管理 ━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.6 模块(菜单)管理 -Web部分 模块(菜单)管理是整个框架的核心,主要面向系统管理人员与开发人员,对普通用户建议不要授 ...
- Winform开发框架之权限管理系统的改进
权限管理系统,一直是很多Mis系统和一些常见的管理系统所需要的,所以一般可以作为独立的模块进行开发,需要的时候进行整合即可,不需要每次从头开发,除非特殊的系统需求.我在Winform开发框架介绍中的随 ...
- Winform界面中实现菜单列表的动态个性化配置管理
在我们一般的应用系统里面,由于系统是面向不同类型的用户,我们所看到的菜单会越来越多,多一点的甚至上百个,但是我们实际工作接触的菜单可能就是那么几个,那么对于这种庞大的菜单体系,寻找起来非常不便.因此对 ...
- SNF开发平台WinForm之四-开发-主细表管理页面-SNF快速开发平台3.3-Spring.Net.Framework
4.1运行效果: 4.2开发实现: 4.2.1 有了第一个程序的开发,代码生成器的配置应该是没有问题了,我们只要在对应的数据库中创建我们需要的表结构就可以了,如下: 主表结构如下: ...
- Winform开发框架之权限管理系统改进的经验总结(2)-用户选择界面的设计
在上篇总结随笔<Winform开发框架之权限管理系统改进的经验总结(1)-TreeListLookupEdit控件的使用>介绍了权限管理模块的用户管理部分,其中主要介绍了其中的用户所属公司 ...
- java—不同的用户登录以后可以看到不同的菜单(后台可以实现对用户菜单的管理) 1 (55)
实现不同的用户登录以后可以看到不同的菜单.(后台可以实现对用户菜单的管理.) 第一步:分析数据结构 1:用户表 表名:users 列名 类型 说明 id Varchar(32) 主键 n ...
随机推荐
- NoSQL生态系统——事务机制,行锁,LSM,缓存多次写操作,RWN
13.2.4 事务机制 NoSQL系统通常注重性能和扩展性,而非事务机制. 传统的SQL数据库的事务通常都是支持ACID的强事务机制.要保证数据的一致性,通常多个事务是不可能交叉执行的,这样就导致了可 ...
- java selenium (四) 使用浏览器调试工具
在基于UI元素的自动化测试中, 无论是桌面的UI自动化测试,还是Web的UI自动化测试. 首先我们需要查找和识别UI元素. 在基于Web UI 自动化测试中, 测试人员需要了解HTML, CSS和 ...
- lock
#ifndef lock_h #define lock_h #include <stdint.h> #include <string.h> #include "myd ...
- 6、HTML5表单提交和PHP环境搭建
---恢复内容开始--- 1.块元素 块元素在显示的时候,通常会以新行开始 如:<h1> <p> <ul> <!-- 块—>注释 <p>he ...
- Can't connect to MySQL server on '127.0.0.1' (111)
[root@localhost ~]# service mysqld statusmysqld 已停 (1)查看MySQL 服务是否已经开启: service mysqld status (2)启动 ...
- DevExpress--xtraTabbedMdiManager控件
因项目需要要实现类似jquery的Tab效果,所以要用到xtraTabbedMdiManager控件 使用xtraTabbedMdiManager一般配合navBarControl(上期已写过) 在工 ...
- gridview的rowdeleting这个函数总是不执行
今天在做新闻管理时,管理数据的时候需要弹出确认删除的功能,可是此功能总是不能够实现,调试的时候也执行不到该方法,后来方向是忘记给button加上一个属性: 把CommandName设置为delete. ...
- Android深度探索(卷1)HAL与驱动开发
第一章 介绍Android驱动开发和移植技术 主要对android和linux做了总体的介绍,让我们有了个感性的认识. 一.Android的四层系统架构: a) Linux内核:Android是基于L ...
- C# WebBrowser控件使用教程与技巧收集
常用的方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(strin ...
- innerHTML属性
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...