一、界面:

二、数据库访问类:

  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实现对权限菜单进行管理的更多相关文章

  1. Winform开发框架之权限管理系统功能介绍

    权限管理系统的重要特性总结: 1) 高度集成的权限系统.独立模块,能快速整合使用.2) 符合权限的国际通用标准,基于RBAC(基于角色的访问控制)的角色权限控制.3) 多数据库架构支持,内置支持Sql ...

  2. 基于吉日嘎底层架构的Web端权限管理操作演示-菜单模块管理

    按照顺序,这一篇介绍菜单模块管理,主要演示如下操作: 新增.修改.锁定.解锁.删除.撤销删除 排序 角色成员管理 用户成员管理 导出菜单模块数据 也许你会问,你在这自吹自擂,你这个BS的权限管理有啥缺 ...

  3. JavaEE权限管理系统的搭建(五)--------RBAC权限管理中的权限菜单的显示

    上一小节实现了登录的实现,本小节实现登录后根据用户名查询当前用户的角色所关联的所有权限,然后进行菜单的显示.登录成功后,如下图所示,管理设置是一级菜单,管理员列表,角色管理,权限管理是二级菜单. 先来 ...

  4. RDIFramework.NET ━ 9.6 模块(菜单)管理 ━ Web部分

    RDIFramework.NET ━ .NET快速信息化系统开发框架 9.6  模块(菜单)管理 -Web部分  模块(菜单)管理是整个框架的核心,主要面向系统管理人员与开发人员,对普通用户建议不要授 ...

  5. Winform开发框架之权限管理系统的改进

    权限管理系统,一直是很多Mis系统和一些常见的管理系统所需要的,所以一般可以作为独立的模块进行开发,需要的时候进行整合即可,不需要每次从头开发,除非特殊的系统需求.我在Winform开发框架介绍中的随 ...

  6. Winform界面中实现菜单列表的动态个性化配置管理

    在我们一般的应用系统里面,由于系统是面向不同类型的用户,我们所看到的菜单会越来越多,多一点的甚至上百个,但是我们实际工作接触的菜单可能就是那么几个,那么对于这种庞大的菜单体系,寻找起来非常不便.因此对 ...

  7. SNF开发平台WinForm之四-开发-主细表管理页面-SNF快速开发平台3.3-Spring.Net.Framework

    4.1运行效果: 4.2开发实现: 4.2.1          有了第一个程序的开发,代码生成器的配置应该是没有问题了,我们只要在对应的数据库中创建我们需要的表结构就可以了,如下: 主表结构如下: ...

  8. Winform开发框架之权限管理系统改进的经验总结(2)-用户选择界面的设计

    在上篇总结随笔<Winform开发框架之权限管理系统改进的经验总结(1)-TreeListLookupEdit控件的使用>介绍了权限管理模块的用户管理部分,其中主要介绍了其中的用户所属公司 ...

  9. java—不同的用户登录以后可以看到不同的菜单(后台可以实现对用户菜单的管理) 1 (55)

    实现不同的用户登录以后可以看到不同的菜单.(后台可以实现对用户菜单的管理.) 第一步:分析数据结构        1:用户表 表名:users 列名 类型 说明 id Varchar(32) 主键 n ...

随机推荐

  1. Shell Script (2) - global.sh

    cd web/ui npm run e2e-dev -- -i 1 "should be able to add function with argument to a ingredient ...

  2. 用 QGIS 画矢量交通路线图

    一.准备工作 1.安装插件 为了方便画图,我们安装了OpenLayers,QuickOSM两个插件. 如何安装插件,度娘上都有答案.下图中打勾的部分为安装好的插件: OpenLayers提供了一些开放 ...

  3. js正则表达式大全(4)

    正则表达式在javascript中的几个实例1(转) ! 去除字符串两端空格的处理 如果采用传统的方式,就要可能就要采用下面的方式了 //清除左边空格 function js_ltrim(destst ...

  4. 从客户端(txtContent="<p>1</p>")中检测到有潜在危险的 Request.Form 值

    输入1也报这个错误, <pages validateRequest="false" 改了也不行,在页头改也不行.到底什么情况呢? 从这个地方找到了答案:http://nt.d ...

  5. CALayer 4 详解 -----转自李明杰

    CALayer4-自定义层   本文目录 一.自定义层的方法1 二.自定义层的方法2 三.其他 自定义层,其实就是在层上绘图,一共有2种方法,下面详细介绍一下. 回到顶部 一.自定义层的方法1 方法描 ...

  6. FPGA作为从机与STM32进行SPI协议通信---Verilog实现 [转]

    一.SPI协议简要介绍 SPI,是英语Serial Peripheral Interface的缩写,顾名思义就是串行外围设备接口.SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用 ...

  7. UVA 11297 线段树套线段树(二维线段树)

    题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要  不同的处理方式,非叶子形成的 ...

  8. VisualSVN-5.1.4补丁原创发布

    VisualSVN-5.1.4补丁原创发布 VisualSVN-5.1.4Patch.rar  VisualSVN-5.1.4官方安装包.rar

  9. linux下如何使用sftp命令

    sftp 是一个交互式文件传输程式.它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性.下边就简单介绍一下如何远程连接主机,进行文件的上传和下载,以及一些相关操作. 举例,如远程主机的 IP ...

  10. 使用CSS3动画属性实现360°无限循环旋转【代码片段】

    使用CSS3的animation动画属性实现360°无限循环旋转. 代码片段: <div id="test"> <img src="/CSS3/img/ ...