C# 小型数据库管理界面

C#第四天  写了一个数据库管理界面 包含到很多知识 也学到很多存一下 感觉搞得有点偏快  记录一下爬忘  初学者代码较low 

放一下 :)   背单词

数据库比较轻量,用的Access (学长连接好的 写好的类和方法)  是一个三级level1 level2 level3结构   

level1条目较少12条吧永恒不变 存到内存了     level2有独立的序列id,从属于第几条lvele1,本身的content,   还有存取的level3的多内容 以;;做分割符(条3专门写了个C++处理字符)


C# 小型数据库管理界面_第1张图片

功能包含后两级的增删改

from布局大概也是这样 三级三列显示 name第一列label+"level1id"   第二列 Level2Label+"level2id"  第三列Level3Lable+"level3id" 

动态生成Label控件实现 操作(不知道优秀的C#程序员用什么方法)

页面的布局大概是这样


在作业项目和危险点分析下面有

作业总则定死的  作业项目 危险点分析是同态生成的Label控件

C# 小型数据库管理界面_第2张图片

                                tableLayoutPanel1                                         Level2_panel                               Level3_panel

很low吧  - - 

实现增添删

删除直接提示 删除然后 

其中增加 修改 用到另一个含有输入框的LavelAllAdd窗体

删除就直接在DBManager上了

下面是DBManager代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace power_project.AppForm
{
    public partial class DBManager : Form
    {
        public String TmpLabName ="1";   
        public String TmpLv2Name = "1";
        public String TmpLv3Name = "1";   //当前选中项
        //String Level2Color = "0";     //记录颜色变化
        //String Level3Color = "0";
        power_project.Public_Class.DataAccessClass MyDataAccess = new Public_Class.DataAccessClass();  // 创建数据库对象
        public DBManager()
        {
            InitializeComponent();
        }
        

        private void DBManager_Load(object sender, EventArgs e)
        {

        }


     // 模拟hover特效
        private void Level1_Enter(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            label.ForeColor = Color.Red;
        }

        private void Level1_Leave(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            label.ForeColor = Color.MediumBlue;
        }
       
        // endhover
        private void Level1_Click(object sender, EventArgs e)
        {
            level2_panel.Controls.Clear();
            int Level2_Rows = 0;
            Label label = (Label)sender;
            TmpLabName = label.Name.Substring(5);
            //MessageBox.Show(TmpLabName);
            OleDbDataReader tmpDR = MyDataAccess.Read_Cmd("select * from tb_user_privilege where (level1_belong =" + TmpLabName+") order by id ASC");
            while(tmpDR.Read())
            {   Level2_Rows++;
                Label lb = new Label();
                lb.Name = "Level2Lable" + Level2_Rows;
                lb.Text = TmpLabName+"-"+Level2_Rows+":"+tmpDR["level2_des"].ToString();
                
                lb.Width = 400;
                lb.Height = 35;
                lb.Click += new EventHandler(this.Level2_Click);
                lb.BackColor = Color.White;
                Point TP = new Point(0, 40 * (Level2_Rows - 1));
                lb.Location = TP;     
                level2_panel.Controls.Add(lb);
            }
        }
       public void Level2_Click(object sender, EventArgs e)
        {
            TmpLv3Name = "1";    //初始化三 防止越界
            level3_panel.Controls.Clear();         //清空布局
            Label label = (Label)sender;
            Control TmpLabForWhite = Controls.Find("Level2Lable" + TmpLv2Name, true)[0];      //清除先前设置的红色
            TmpLabForWhite.BackColor = Color.White;
            TmpLv2Name = label.Name.Substring(11);
            TmpLabForWhite = Controls.Find("Level2Lable" + TmpLv2Name, true)[0];      //清除先前设置的红色
            TmpLabForWhite.BackColor = Color.DodgerBlue ;
            OleDbDataReader tmpDR = MyDataAccess.Read_Cmd("select * from tb_user_privilege where level1_belong =" + TmpLabName + "and level2_id = " + TmpLv2Name);
            if(tmpDR.Read())               //给leve3创建label
            {   
                string content = tmpDR["level3_des"].ToString();
                string[] sArr = Regex.Split(content, ";;");
                    for(int i=0;i
下面是LevelAllAdd代码
窗体传递参数  用的是构造函数 (网上查也可用 public成员,还有另一种方法)
修改level2级内容 是有三个String参数的构造 ,level3直接表示操作
修改level3级内容 是有四个String参数的构造,levle3operate表示操作

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Text.RegularExpressions;
namespace power_project.AppForm
{
   
    public partial class LevelAllAdd : Form
    {
        power_project.Public_Class.DataAccessClass MyDataAccess = new Public_Class.DataAccessClass();
        String level1="0";
        String level2="0";
        String level3="0";
        String level3operate = "";
        public LevelAllAdd()
        {
            InitializeComponent();
        }
        public LevelAllAdd(String level1,String level2,String level3)
        {
            this.level1 = level1;
            this.level2 = level2;
            this.level3 = level3;
            InitializeComponent();
        }
        public LevelAllAdd(String level1, String level2, String level3,String level3operate)
        {
            this.level1 = level1;
            this.level2 = level2;
            this.level3 = level3;
            this.level3operate = level3operate;
            InitializeComponent();
        }
        public int SplitForSemiconlon(String str, int IndexSemiconlonOld)
        {
          
           int IndexSemiconlonNew = str.IndexOf(";;", IndexSemiconlonOld );   //寻找;;的下标位置
            return IndexSemiconlonNew;
        }
        private void LevelAllAdd_Load(object sender, EventArgs e)
        { if (level3.Equals("level2update"))
            {
                OleDbDataReader tmpDRForShow = MyDataAccess.Read_Cmd("select *from tb_user_privilege where level1_belong= " + level1 + " and level2_id=" + level2);
                tmpDRForShow.Read();
                AddTextBox.Text = tmpDRForShow["level2_des"].ToString();
            }
          else if(level3operate.Equals("level3update"))
            {
                OleDbDataReader tmpDRForShow = MyDataAccess.Read_Cmd("select *from tb_user_privilege where level1_belong= " + level1 + " and level2_id=" + level2);
                tmpDRForShow.Read();
                string content = tmpDRForShow["level3_des"].ToString();
                string[] sArr = Regex.Split(content, ";;");
                AddTextBox.Text = sArr[Convert.ToInt32(level3)-1].ToString();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        { if(level3.Equals("level2add"))             //添加level2条
            {
                OleDbDataReader tmpDRForCount = MyDataAccess.Read_Cmd("select count(level2_id) as level2row from tb_user_privilege where level1_belong=" + level1);   //统计条数 编号level2_id
                tmpDRForCount.Read();
                String Level2Rows = tmpDRForCount["level2row"].ToString();
                int ConveyNum = Convert.ToInt32(Level2Rows)+1;   //将string类型转换为int类型
                Level2Rows = ConveyNum.ToString();
                //MessageBox.Show(Level2Rows);
                //MessageBox.Show(level1);
                //MessageBox.Show(AddTextBox.Text);
                OleDbDataReader tmpDR = MyDataAccess.Read_Cmd("insert into tb_user_privilege(level1_belong,level2_des,level2_id) values (" + level1 + ",'" + AddTextBox.Text + "',"+Level2Rows+")");

                MessageBox.Show("添加成功:)");
                this.Close();
            }
          else if(level3.Equals("level2update"))    //更新level2条
            {
                //MessageBox.Show(level1);
                //MessageBox.Show(level2);
                //MessageBox.Show(AddTextBox.Text);
                //MessageBox.Show("update tb_user_privilege set level2_des='" + AddTextBox.Text + "' where (level1_belong = " + level1 + " and level2_id = " + level2 + ")");
              

                OleDbDataReader tmpDR = MyDataAccess.Read_Cmd("update tb_user_privilege set level2_des='"+AddTextBox.Text+"' where (level1_belong = "+level1+" and level2_id = "+level2+")");
                MessageBox.Show("信息更新成功");
                this.Close();
            }
          else                                      //level3操作
            {
                //MessageBox.Show(level3operate);          //先读取level3des
                OleDbDataReader tmpDR = MyDataAccess.Read_Cmd("select *from tb_user_privilege where level1_belong= "+level1+" and level2_id="+level2);
                tmpDR.Read();
                  if (level3operate.Equals("level3add"))   //level3增加操作
                  {
                    
    
                    String TmpLevel3DesForAdd = tmpDR["level3_des"].ToString()+AddTextBox.Text+";;";
                    //MessageBox.Show("修改后的信息" + TmpLevel3DesForAdd);
                    OleDbDataReader tmpDR2 = MyDataAccess.Read_Cmd("update tb_user_privilege set level3_des ='"+TmpLevel3DesForAdd+" '  where level1_belong= "+level1+" and level2_id= "+level2);
                    MessageBox.Show("信息添加成功");
                    this.Close();
                   }
                  else  if (level3operate.Equals("level3update"))        //level3更新操作
                {
                    
                    String TmpLevel3Update = tmpDR["level3_des"].ToString();
                    int TmpIndexst = 0;               //相应子串起始位置
                    int TmpIndexen = 0;                 //相应字串终止位置
                    //MessageBox.Show(TmpLevel3Update);
                    //MessageBox.Show("leve3更新操作"); 
                    for(int i=0;i

效果就是后两级内容增删的功能实现了 3涉及一些分串操作(哎只能传<2Mb的)

C# 小型数据库管理界面_第3张图片

你可能感兴趣的:(C#)