一、界面:

二、数据库访问类:

  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. 常看常遇见之一——BS架构VS CS架构

    常看常遇见之一——BS架构VS CS架构 1.BS架构 即Browser/Server(浏览器/服务器)结构,是随着Internet技术的兴起,对C/S结构的一种变化或者改进的结构.在这种结构下,用户 ...

  2. 隔壁信概大作业xjb写——同化棋ATAXX

    话说泥萌北大信科啊,助教是有多懒...去年黑白棋今年同化棋,顺带打ai都不用自己写标程... 好吧..我知道泥萌重点在各种sb的辅助操作上..什么悲剧的可以随时暂停载入...有毒吧 [据说泥萌上课没讲 ...

  3. 在Heroku上,安装Wordpress

    其實在 Heroku 上安裝 Wordpress 不會很難,不過閱讀之前,你可能先要知道 Heroku 與 git 的基本操作,建議可以先參考以下網站用 Heroku 架設 Wordpress 網站 ...

  4. String字符串类课后作业

    String动手动脑和课后作业 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 结果: 总结:在Java中,内容相同的字串常量(&quo ...

  5. 商品sku规格选择效果,没有商品的不能选中,选择顺序不影响展示结果

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  6. 最近用到js筛选一个url的域名部分(草创)

    var TLD = ['com','net','org','gov','edu','mil','biz','name','info','mobi','cn','hk']; var host = ''; ...

  7. 一次线上http接口调用不通相关的解决过程

    2016-05-25 08:58:34 昨天线上小白系统因为调用外部http接口,超时不释放,导致页面反应很慢,时间一长,报502错误. 上网查了下,502错误是因为服务对于客户的请求没有得到及时的反 ...

  8. 更新centos curl

    centos curl 默认使用nss,而不是openssl 升级curl让curl支持openssl rpm -Uvh http://www.city-fan.org/ftp/contrib/yum ...

  9. Java_DOM创建XML

    import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream; ...

  10. Ajax与Comet

    1.Ajax核心? XHR >>1.新建XMLHttpRequest >>2.open(),接受3个参数. >>3.send(),接受1个参数. >>4 ...