zoukankan      html  css  js  c++  java
  • Winform 学生管理系统增删改查

    数据库:

    create database adonet
    go
    use adonet
    go
    
    create table xue
    (
       code nvarchar(50),
       name nvarchar(50),
       sex bit,
       birth datetime,
       chengji decimal(10,2)
    )
    
    insert into xue values('s101','张三',1,'1995-3-2',99)
    insert into xue values('s102','李四',1,'1995-4-2',89)
    insert into xue values('s103','王五',1,'1994-3-8',95)
    insert into xue values('s104','赵六',1,'1993-8-4',79)
    insert into xue values('s105','小红',0,'1996-5-11',68)

    VS代码部分:

    数据库实体类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 学生管理系统.数据库实体
    {
        public class student
        {
            private string _code;
    
            public string Code
            {
                get { return _code; }
                set { _code = value; }
            }
            private string _name;
    
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            private bool _sex;
    
            public bool Sex
            {
                get { return _sex; }
                set { _sex = value; }
            }
            private DateTime _birthday;
    
            public DateTime Birthday
            {
                get { return _birthday; }
                set { _birthday = value; }
            }
            private decimal _score;
    
            public decimal Score
            {
                get { return _score; }
                set { _score = value; }
            }
        }
    }

    数据库访问操作类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using 学生管理系统.数据库实体;
    using System.Data.SqlClient;
    
    namespace 学生管理系统.数据库访问
    {
        public class studentdata
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
    
            public studentdata() 
            {
                conn =new SqlConnection("server=.;database=adonet;user=sa;pwd=123;");
                cmd = conn.CreateCommand();
            }
    
            /// <summary>
            /// 查询全部学生的信息
            /// </summary>
            /// <returns></returns>
            public List<student> selectall() 
            {
                List<student> li = new List<student>();
                cmd.CommandText = "select * from xue";
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows) 
                {
                    while (dr.Read()) 
                    {
                        student st = new student();
                        st.Code = dr["code"].ToString();
                        st.Name = dr["name"].ToString();
                        st.Sex = Convert.ToBoolean(dr["sex"]);
                        st.Birthday = Convert.ToDateTime(dr["birth"]);
                        st.Score = Convert.ToDecimal(dr["chengji"]);
    
                        li.Add(st);
                    }
                }
                conn.Close();
                return li;
            }
    
            /// <summary>
            /// 返回一个新的学生编号
            /// </summary>
            /// <returns></returns>
            public string fcode() 
            {
                string code = "";
                List<student> li = selectall();//引用一下查询学生全部信息
    
                List<int> max = new List<int>();//用来选取学生最大编号
                foreach (student s in li) //遍历学生信息
                {
                    //只点出编号,截取字符串一号索引之后的,因为学号的格式为s101,需要截取字母后面的,以便下一步操作
                    max.Add(Convert.ToInt32(s.Code.Substring(1)));//将截取完的所有学生编号放进集合
                }
                //冒泡排序,将截取后的整数类型的学生最大编号排出来,此时最大编号在索引0上
                for (int i = 0; i < max.Count-1; i++) 
                {
                    for (int j = i + 1; j < max.Count; j++) 
                    {
                        if (max[i] < max[j]) 
                        {
                            int zhong = max[i];
                            max[i] = max[j];
                            max[j] = zhong;
                        }
                    }
                }
    
                code = "s" +( max[0] + 1);//每次新的编号就是最大编号的整数部分加1,前面再拼上s编号格式
                return code;
            }
    
            /// <summary>
            /// 新增学员信息
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public bool insert(student s) 
            {
                bool isok = false;
                cmd.CommandText = "insert into xue values(@code,@name,@sex,@birth,@score)";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@code",s.Code);
                cmd.Parameters.Add("@name",s.Name);
                cmd.Parameters.Add("@sex", s.Sex);
                cmd.Parameters.Add("@birth", s.Birthday);
                cmd.Parameters.Add("@score", s.Score);
                conn.Open();
                int count= cmd.ExecuteNonQuery();
                conn.Close();
                if (count > 0) 
                {
                    isok = true;
                }
                return isok;
            }
    
    
            /// <summary>
            /// 修改学生信息
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public bool update(student s) 
            {
                bool isok = false;
                cmd.CommandText = "update xue set name=@name,sex=@sex,birth=@birth,chengji=@chengji where code=@code ";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@code", s.Code);
                cmd.Parameters.Add("@name", s.Name);
                cmd.Parameters.Add("@sex", s.Sex);
                cmd.Parameters.Add("@birth", s.Birthday);
                cmd.Parameters.Add("@chengji", s.Score);
                conn.Open();
                int count= cmd.ExecuteNonQuery();
                conn.Close();
                if (count > 0) 
                {
                    isok = true;
                }
                return isok;
            }
    
    
            /// <summary>
            /// 删除学生信息
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public bool delete(student s)
            {
                bool isok=false;
                cmd.CommandText = "delete from xue where code=@code";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@code",s.Code);
                conn.Open();
                int count = cmd.ExecuteNonQuery();
                conn.Close();
                if (count >0)
                {
                    isok = true;
                }
                return isok;
                
            }
        }
    }

    主窗体:

    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 学生管理系统.数据库实体;
    using 学生管理系统.数据库访问;
    
    
    namespace 学生管理系统
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                shuaxin();//在构造函数中调用查询显示的方法,每次一进来都刷新显示一遍
            }
    
            public void shuaxin()//将数据库查询显示在 listView中
            {
                listView1.Items.Clear();//每次查询显示都清空一次listView集合,只显示新的数据
                List<student> data = new studentdata().selectall();//调用数据库操作访问类中的查询方法方法
                foreach (student st in data) //遍历一下操作类方法中返回的值
                {
                    ListViewItem lis = new ListViewItem();//new一个ListView集合,将返回的集合放进去
                    lis.Text = st.Code;
                    lis.SubItems.Add(st.Name);
                    lis.SubItems.Add(st.Sex?"":"");
                    lis.SubItems.Add(st.Birthday.ToString("yyyy年MM月dd日"));
                    lis.SubItems.Add(st.Score.ToString());
    
                    listView1.Items.Add(lis);
                }
            }
    
            List<Form> f = new List<Form>();//写一个泛型集合将所有窗体放进去
            Form2tj f2 = null;
            private void button1_Click(object sender, EventArgs e)
            {
                //点击添加按钮后将之前写好的添加窗体实例化出来,
                f2 = new Form2tj(this);//因为添加窗体中利用传参的方法引用了此窗体,所以实例化的添加窗体也需要将本窗体放进参数
                bool has = false;
                foreach (Form ff in f)//为了保证弹出窗体的唯一性,也就是点击只弹出这一个窗体,再次点击不会重复弹出
                {                //那就需要遍历所有From窗体
                    if (ff.Name == f2.Name)//然后进行进行判断,如果在原有的窗体中有和新开窗体名称相同的
                    {
                        has = true;
                        ff.WindowState = FormWindowState.Normal;//将窗口的显示状态执行初始状态,也就是最小化,最大化都会执行还原
                        ff.Show();//那就显示原有的窗口
                        ff.Activate();//激活窗体并给予它焦点,这一步就是用于点击打开的窗体获得焦点显示在最前端
                        f2.Close();//将新开的窗口关闭
                    }
                    if (ff.Name != f2.Name) //把其他窗口隐藏掉
                    {
                        ff.Hide();
                    }
                }
                if (has == false) //如果原有窗体没有与新开窗体同名的
                {
                    f2.Show();//那就将新窗体执行开启显示
                    f.Add(f2);//并且将这个窗体放进集合中,下次再点击这个按钮就会执行上面的步骤
                }
                
            }
    
    
            //根据以上写法,已经打开过一次的窗体就存在于Form窗体集合中,
            //那关闭这个窗体再点击弹出这个窗体的按钮时就会报错,以为已经存在这个窗体
            //所以要写一个方法,让弹出的窗体关闭时同时也将此窗体从窗体集合中移除
            public void deletef2() 
            {
                List<Form> fr = new List<Form>();//创建窗体类的泛型集合
                foreach (Form fd in f) //遍历f这个窗体集合
                {
                    if (fd.Name != f2.Name) //判断一下所有和新开窗体不同名的窗体
                    {
                        fr.Add(fd);//将这些窗体放入新创建的集合,
                    }
                }
                f = fr;//再把这个集合赋值给原来的窗体集合,新赋值进去的覆盖了原有的,
            }          //这样,就把这个窗体从集合中移除,然后再去这个窗体的后台代码中写一个关闭前事件调用此方法
    
    
    
            //修改按钮点击事件
            Form3xg f3 = null;
            private void button2_Click(object sender, EventArgs e)
            {
                student s = new student();
                if (listView1.SelectedItems.Count != 1) //SelectedItems获取控件中选中的项
                {
                    MessageBox.Show("每次只能选择一个同学进行修改!");
                    return;
                }
    
                foreach (ListViewItem li in listView1.SelectedItems) 
                {
                    s.Code = li.Text;
                    s.Name = li.SubItems[1].Text;
                    if (li.SubItems[2].Text == "")
                    {
                        s.Sex = true;
                    }
                    else 
                    {
                        s.Sex = false;
                    }
                    s.Birthday = Convert.ToDateTime(li.SubItems[3].Text);
                    s.Score = Convert.ToDecimal(li.SubItems[4].Text);
                }
    
                //弹出窗体的唯一性
                f3 = new Form3xg(s,this);
                bool has = false;
                foreach (Form ff in f)//为了保证弹出窗体的唯一性,也就是点击只弹出这一个窗体,再次点击不会重复弹出
                {                //那就需要遍历所有From窗体
                    if (ff.Name == f3.Name)//然后进行进行判断,如果在原有的窗体中有和新开窗体名称相同的
                    {
                        has = true;
                        ff.WindowState = FormWindowState.Normal;//将窗口的显示状态执行初始状态,也就是最小化,最大化都会执行还原
                        ff.Show();//那就显示原有的窗口
                        ff.Activate();//激活窗体并给予它焦点,这一步就是用于点击打开的窗体获得焦点显示在最前端
                        f3.Close();//将新开的窗口关闭
                    }
                    if (ff.Name != f3.Name)
                    {
                        ff.Hide();
                    }
                }
                if (has == false) //如果原有窗体没有与新开窗体同名的
                {
                    f3.Owner = this;//拥有者,这个窗体显示在前面
                    f3.Show();//那就将新窗体执行开启显示
                    f.Add(f3);//并且将这个窗体放进集合中,下次再点击这个按钮就会执行上面的步骤
                }
            }
    
            public void deletef3()
            {
                List<Form> fr = new List<Form>();//创建窗体类的泛型集合
                foreach (Form fd in f) //遍历f这个窗体集合
                {
                    if (fd.Name != f3.Name) //判断一下所有和新开窗体不同名的窗体
                    {
                        fr.Add(fd);//将这些窗体放入新创建的集合,
                    }
                }
                f = fr;//再把这个集合赋值给原来的窗体集合,新赋值进去的覆盖了原有的,
            }
    
    
    
            //删除按钮点击事件
            Form4sc f4 = null;
            private void button3_Click(object sender, EventArgs e)
            {
                student s = new student();
                if (listView1.SelectedItems.Count != 1) //SelectedItems获取控件中选中的项
                {
                    MessageBox.Show("每次只能选择一个同学进行删除!");
                    return;//返回出去
                }
    
                foreach (ListViewItem li in listView1.SelectedItems)
                {
                    s.Code = li.Text;
                    s.Name = li.SubItems[1].Text;
                    if (li.SubItems[2].Text == "")
                    {
                        s.Sex = true;
                    }
                    else
                    {
                        s.Sex = false;
                    }
                    s.Birthday = Convert.ToDateTime(li.SubItems[3].Text);
                    s.Score = Convert.ToDecimal(li.SubItems[4].Text);
                }
                f4 = new Form4sc(s,this);
                //弹出窗体的唯一性
                bool has = false;
                foreach (Form ff in f)//为了保证弹出窗体的唯一性,也就是点击只弹出这一个窗体,再次点击不会重复弹出
                {                //那就需要遍历所有From窗体
                    if (ff.Name == f4.Name)//然后进行进行判断,如果在原有的窗体中有和新开窗体名称相同的
                    {
                        has = true;
                        ff.WindowState = FormWindowState.Normal;//将窗口的显示状态执行初始状态,也就是最小化,最大化都会执行还原
                        ff.Show();//那就显示原有的窗口
                        ff.Activate();//激活窗体并给予它焦点,这一步就是用于点击打开的窗体获得焦点显示在最前端
                        f4.Close();//将新开的窗口关闭
                    }
                    if (ff.Name != f4.Name)
                    {
                        ff.Hide();
                    }
                }
                if (has == false) //如果原有窗体没有与新开窗体同名的
                {
                    f4.Owner = this;//拥有者,这个窗体显示在前面
                    f4.Show();//那就将新窗体执行开启显示
                    f.Add(f4);//并且将这个窗体放进集合中,下次再点击这个按钮就会执行上面的步骤
                }
            }
    
            public void deletef4()
            {
                List<Form> fr = new List<Form>();//创建窗体类的泛型集合
                foreach (Form fd in f) //遍历f这个窗体集合
                {
                    if (fd.Name != f4.Name) //判断一下所有和新开窗体不同名的窗体
                    {
                        fr.Add(fd);//将这些窗体放入新创建的集合,
                    }
                }
                f = fr;//再把这个集合赋值给原来的窗体集合,新赋值进去的覆盖了原有的,
            }
        }
    }

    添加学生窗体:

    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 学生管理系统.数据库实体;
    using 学生管理系统.数据库访问;
    
    namespace 学生管理系统
    {
        public partial class Form2tj : Form
        {
            //窗体连接操作,比如此窗体Form2tj要连接调用主窗体Form1,则需要
            Form1 F1 = null;//建立一个空的Form1对象
            public Form2tj(Form1 fi)//将主窗体以传参的形式传进来
            {
                InitializeComponent();
                label_code.Text = new studentdata().fcode();
                F1=fi;//然后将传进来的Form1窗体赋值给自己建立的Form1对象,这样通过F1就可以操作Form1窗体中的方法
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if(string.IsNullOrEmpty(textBox_name.Text))
                {
                    name_cw.Text="不能为空";
                    return;
                }
                else
                {
                    name_cw.Text="";
                }
                student s = new student();
                s.Code= label_code.Text;
                s.Name = textBox_name.Text;
                s.Sex = radioButton_nan.Checked;
                s.Birthday = dateTimePicker1.Value;
                s.Score=Convert.ToDecimal(textBox_score.Text);
    
                bool isok=new studentdata().insert(s);
                if(isok==true)
                {
                    MessageBox.Show("添加成功!");
                    if(F1!=null)
                    {
                        F1.shuaxin();
                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败!");
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                textBox_name.Text = "";
                radioButton_nan.Checked = true;
                dateTimePicker1.Value = DateTime.Now;
                textBox_score.Text = "";
            }
    
    
            //FormClosing关闭前事件
            private void Form2tj_FormClosing(object sender, FormClosingEventArgs e)
            {
                F1.deletef2();//调用form1中写的方法
            }
        }
    }

    修改学生窗体:

    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 学生管理系统.数据库实体;
    using 学生管理系统.数据库访问;
    
    namespace 学生管理系统
    {
        public partial class Form3xg : Form
        {
            Form1 F1 = null;
            public Form3xg(student s,Form1 f1)
            {
                InitializeComponent();
                label_code.Text = s.Code;
                textBox_name.Text = s.Name;
                if (s.Sex == true)
                {
                    radioButton_nan.Checked = true;
                }
                else 
                {
                    radioButton_nv.Checked = true;
                }
                dateTimePicker1.Value = s.Birthday;
                textBox_score.Text = s.Score.ToString();
                F1 = f1;
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(textBox_name.Text))
                {
                    name_cw.Text = "不能为空";
                    return;
                }
                else
                {
                    name_cw.Text = "";
                }
                student s = new student();
                s.Code = label_code.Text;
                s.Name = textBox_name.Text;
                s.Sex = radioButton_nan.Checked;
                s.Birthday = dateTimePicker1.Value;
                s.Score = Convert.ToDecimal(textBox_score.Text);
    
                bool isok = new studentdata().update(s);
                if (isok == true)
                {
                    MessageBox.Show("修改成功!");
                    if (F1 != null)
                    {
                        F1.shuaxin();
                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败!");
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                textBox_name.Text = "";
                radioButton_nan.Checked = true;
                dateTimePicker1.Value = DateTime.Now;
                textBox_score.Text = "";
            }
    
            private void Form3xg_FormClosing(object sender, FormClosingEventArgs e)
            {
                F1.deletef3();
            }
        }
    }

    删除学生窗体:

    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 学生管理系统.数据库实体;
    using 学生管理系统.数据库访问;
    
    namespace 学生管理系统
    {
        public partial class Form4sc : Form
        {
            Form1 F1 = null;
            public Form4sc(student s,Form1 f1)
            {
                InitializeComponent();
                label_code.Text = s.Code;
                textBox_name.Text = s.Name;
                if (s.Sex == true)
                {
                    radioButton_nan.Checked = true;
                }
                else
                {
                    radioButton_nv.Checked = true;
                }
                dateTimePicker1.Value = s.Birthday;
                textBox_score.Text = s.Score.ToString();
                F1 = f1;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                student s = new student();
                s.Code = label_code.Text;
                bool isok = new studentdata().delete(s);
                if (isok == true)
                {
                    MessageBox.Show("删除成功!");
                    if (F1 != null)
                    {
                        F1.shuaxin();
                    }
                    this.Close();
                }
                else 
                {
                    MessageBox.Show("删除失败!");
                }
            }
            //取消按钮点击
            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
            //窗体关闭前执行主窗体中写的将此窗体移出窗体集合的方法,以免再次打开出错
            private void Form4sc_FormClosing(object sender, FormClosingEventArgs e)
            {
                F1.deletef4();
            }
        }
    }

    主窗体:

    添加学生窗体:

    修改学生信息窗体:

    删除学生信息窗体:

  • 相关阅读:
    我cnblogs的主题
    Scala Error: error while loading Suite, Scala signature Suite has wrong version expected: 5.0 found: 4.1 in Suite.class
    Spark之路 --- Scala用JFreeChart画图表实例
    Spark之路 --- Scala IDE Maven配置(使用开源中国的Maven库)和使用
    Spark之路 --- Windows Scala 开发环境安装配置
    epoll函数
    Linux网络编程目录
    函数wait和waitpid
    会话
    进程组
  • 原文地址:https://www.cnblogs.com/zyg316/p/5648890.html
Copyright © 2011-2022 走看看