Newer
Older
GHFX_REFACTOR / SysRescInfoManager.cs
wxn on 9 Nov 2016 5 KB 冗余代码整理
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace Cyberpipe
{
    public partial class SysRescInfoManager : Office2007Form
    {
        public static bool IS_OPEN;
        public SysRescInfoManager()
        {
            InitializeComponent();
        }

        private void SysRescInfoManager_Load(object sender, EventArgs e)
        {
            IS_OPEN = true;
            reloadGrid();
            if (Utility.userRole.IndexOf("资源新增") == -1)
            {
                btn_add.Visible = false;
            }
            if (Utility.userRole.IndexOf("资源删除") == -1)
            {
                dataGridViewX1.Columns["btndel"].Visible = false;
            }
        }

        private void SysRescInfoManager_FormClosing(object sender, FormClosingEventArgs e)
        {
            IS_OPEN = false;
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txt_resc_name.Text.Trim()))
                {
                    MessageBox.Show("资源名称不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                string sql = "insert into casic_userresctest (resc,desp,sysname) values ('" + txt_resc_name.Text.Trim() + "','" + txt_resc_des.Text.Trim() + "','GHFX')";
                OracleUtils.ExecuteNonQuery(OracleUtils.ConnectionString, CommandType.Text, sql);
                reloadGrid();
                MessageBox.Show("新增资源成功!", "结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("新增资源失败:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void reloadGrid()
        {
            string sql = "select id,resc,desp,'删除' as btndel from casic_userresctest where sysname='GHFX' order by id desc";
            DataTable table = OracleUtils.ExecuteDataset(OracleUtils.ConnectionString, CommandType.Text, sql).Tables[0];
            dataGridViewX1.DataSource = table;
        }

        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }
            string buttonText = dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            if (buttonText.Equals("删除"))
            {
                if (MessageBox.Show("确定删除?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string dbid = dataGridViewX1.Rows[e.RowIndex].Cells["id"].Value.ToString();
                    string resc = dataGridViewX1.Rows[e.RowIndex].Cells["resc"].Value.ToString();
                    OracleConnection conn = null;
                    OracleTransaction tran = null;
                    try
                    {
                        conn = new OracleConnection(OracleUtils.ConnectionString);
                        if (conn.State == ConnectionState.Closed)
                        {
                            conn.Open();
                        }
                        tran = conn.BeginTransaction();
                        string sql = "delete from casic_userresctest where id=" + dbid;
                        OracleUtils.ExecuteNonQuery(tran, CommandType.Text, sql);

                        List<string> list = new List<string>();
                        sql = "select id,gid from casic_userroletest where gid like '%" + resc + "%'";
                        using (OracleDataReader reader = OracleUtils.ExecuteReader(tran, CommandType.Text, sql))
                        {
                            while (reader.Read())
                            {
                                list.Add("update casic_userroletest set gid='" + reader["gid"].ToString().Replace(resc + ",", "").Replace(resc, "") + "' where id=" + reader["id"]);
                            }
                        }
                        
                        foreach (string str in list)
                        {
                            OracleUtils.ExecuteNonQuery(tran, CommandType.Text, str);
                        }
                        tran.Commit();
                        MessageBox.Show("删除成功!", "结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        reloadGrid();
                    }
                    catch (Exception ex)
                    {
                        if (null != tran)
                        {
                            tran.Rollback();
                        }
                        MessageBox.Show("删除失败:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        if (null != conn && conn.State == ConnectionState.Open)
                        {
                            conn.Close();
                        }
                    }
                }
            }
        }
    }
}