Newer
Older
EMS_REFACTOR / FrmRole.cs
nn-203 on 26 Jul 2017 5 KB first commit
using System;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace Cyberpipe
{
    public partial class FrmRole : Office2007Form
    {
        int id;
        public FrmRole(int _id)
        {
            InitializeComponent();
            id = _id;
        }
        DataTable table;
        /// <summary>
        /// 窗体初始化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmRoleMgr_Load(object sender, EventArgs e)
        {
            string sql = "select \"id\",\"name\" from casic_perm";
            table = OledbHelper.QueryTable(sql);
            if (table != null)
            {
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        comboBoxEx1.Items.Add(table.Rows[i]["name"].ToString());
                    }
                    if (id != -1)
                    {
                        Text = "修改角色信息";
                        buttonX1.Text = "保存";
                        sql = "select casic_perm.\"name\",casic_role.\"name\",casic_role.\"desc\"  from casic_perm  inner join casic_role on casic_perm.\"id\" = casic_role.\"pid\" where casic_role.\"id\" = " + id;
                        table = OledbHelper.QueryTable(sql);
                        if (table != null && table.Rows.Count > 0)
                        {
                            for (int j = 0; j < comboBoxEx1.Items.Count; j++)
                            {
                                if (comboBoxEx1.Items[j].ToString() == table.Rows[0][0].ToString())
                                {
                                    comboBoxEx1.SelectedIndex = j;
                                    break;
                                }
                            }                            
                            txtDesc.Text = table.Rows[0][2].ToString();                           
                            txtName.Text = table.Rows[0][1].ToString();
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 添加、保存按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (comboBoxEx1.SelectedIndex == -1)
            {
                MessageBox.Show("请选择权限名称!");
                return;
            }
            if (txtName.Text.Trim() == "")
            {
                MessageBox.Show("请输入角色名称!");
                return;
            }
           
            try
            {
                string sql = "select \"id\" from casic_perm where \"name\" = '" + comboBoxEx1.SelectedItem + "'";

                DataTable dt = OledbHelper.QueryTable(sql);
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        int pid = Convert.ToInt32(dt.Rows[0][0].ToString());
                        if (id == -1)
                        {
                            sql = "select * from casic_role where \"name\" ='" + txtName.Text.Trim() + "' and \"pid\" =" + pid;
                            if (OledbHelper.QueryTable(sql).Rows.Count > 0)
                            {
                                MessageBox.Show("该权限下已录入该角色信息!");
                                return;
                            }
                            sql = "insert into casic_role(\"name\",\"desc\",\"pid\") values('" + txtName.Text.Trim() + "','" + txtDesc.Text.Trim() + "'," + pid + ")";
                        }
                        else
                        {
                            if (txtDesc.Text == table.Rows[0][2].ToString() &&                            
                            txtName.Text == table.Rows[0][1].ToString() &&
                            comboBoxEx1.SelectedItem.ToString() == table.Rows[0][0].ToString())
                            {
                                DialogResult = DialogResult.OK;
                                Close();
                                return;
                            }
                            sql = "select * from casic_role where \"name\" ='" + txtName.Text.Trim() + "' and \"pid\" =" + pid + " and \"id\" <>" + id;
                            if (OledbHelper.QueryTable(sql).Rows.Count > 0)
                            {
                                MessageBox.Show("该权限下已录入该角色信息!");
                                return;

                            }
                            sql = "update casic_role set \"name\"='" + txtName.Text.Trim() + "',\"desc\" = '" + txtDesc.Text.Trim() + "',\"pid\" = " + pid + " where \"id\" = " + id;
                        }
                        if (OledbHelper.sqlExecuteNonQuery(sql) > 0)
                        {
                            MessageBox.Show("保存成功!");
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogError.PublishError(ex);
            }
        }
        /// <summary>
        /// 关闭按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}