• 学生管理系统初步总结


    之前我们已经学过了windows窗体应用的一些基本控件.今天我们就用我们用我们所学的一些知识来写一写学生管理系统!

    登录窗体

    image

    首先程序已启动我们所看到的就是登录窗体.它包括文本框、标签、和按钮.那么密码框是怎么实现的呢?

    只需要将文本框的passwordchar属性该成任意但个字符那么这个文本框就会变成密码框并且该密码框中输入的字符会用您设定的字符代替如图所示:image

    将该属性的值设置为’●’那么您在密码框中输入的任意字符都会以’●’代替显示!

     

    窗体的事件

    按钮一般都会出发一些事件,而我们的这个项目中的按钮也有自己的事件.

    其中image按钮只要我们单击该按钮程序就会触发该按钮的Click事件从而验证您的用户名及密码是否正确

    如果正确则跳转到主窗体如果不正确弹出消息框提示您输入错误!如图所示:

    image

    下面我们来看一下具体的实现代码!

    private void button_yes_Click(object sender, EventArgs e)
            {
                string conn = "data source=.;initial catalog=myschool;uid=sa;";//连接数据库的字符串
                SqlConnection con = new SqlConnection(conn);//创建数据库连接对象
                string sql = "select count(1) from student where studentname='" + text_name.Text + "' and loginpwd='"+text_pwd.Text+"'";//让数据库执行的T-sql语句
                SqlCommand cmd = new SqlCommand(sql, con);//创建Command对象
                try
                {
                    con.Open();//打开连接
                    int count = Convert.ToInt32(cmd.ExecuteScalar());//执行查询语句
                    if (count>0)//验证用户名及密码是否正确
                    {
                       
                        myschool_管理员 frm = new myschool_管理员();
                        frm.name = text_name.Text;
                        tool.name = text_name.Text;
                        this.Hide();
                        tool.pwd = text_pwd.Text;
                        frm.Show();
                    }
                    else
                    {
                        MessageBox.Show("用户名或密码错误","温馨提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("系统出错,请稍后再试","温馨提示");
                    
                }
                finally
                {
                    con.Close();//关闭数据库连接
                }
            }

    主窗体

    当我们的用户名和密码都验证通过之后就会跳转到主窗体.现在我们先来看一下主窗体的界面长什么样吧

    image

    这就是我们的主窗体它是由菜单栏和工具栏组成我们程序的主要功能都可以通过它来实现比如点击新建学生信息就会显示添加学生信息窗体.其实现代码如下:

    private void 编辑学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                addstudenng sb = new addstudenng();//创建新曾学生信息窗体的对象
                sb.MdiParent = this;//将他的父窗体设置为主窗体
                sb.Show();//显示该窗体
            }

    其他的窗体显示方法基本上都类同这里就不一一说明了!

     

    新增学生信息窗体

    新增学生窗体是由标签、文本框、下拉框、按钮和日期控件组成如图所示:

    image

    该窗体的功能就是项数据空中添加一条学生记录!其实现代码如下:

    public int  stuid=0;
            public SqlConnection readay()
            {
                string conn = "data source=.;initial catalog=myschool;uid=sa;pwd= ";
                SqlConnection con = new SqlConnection(conn);
                return con;
            }
            //向年级下拉框中绑定数据
            public void load()
            {
                SqlConnection con = readay();
                string sql = "select gradename from grade";
                SqlCommand cmd = new SqlCommand(sql, con);
                try
                {
                    con.Open();
                    SqlDataReader br = cmd.ExecuteReader();
                    if (br != null)
                    {
                        if (br.HasRows)
                        {
                            while (br.Read())
                            {
                                string name = Convert.ToString(br["gradename"]);
                                cmbgrade.Items.Add(name);
                            }
                        }
                    }
                    br.Close();
    
                }
                catch (Exception)
                {
                    MessageBox.Show("网络异常");
    
                }
                finally
                {
    
                    con.Close();
                }
            }
            //根据年级编号获取年级名称
            public void getgradename(string gradeid) 
            {
                SqlConnection conn = readay();
                StringBuilder sqll = new StringBuilder("select gradename from grade");
                sqll.AppendFormat(" where gradeid={0} ", gradeid);
                SqlCommand cmdd = new SqlCommand(sqll.ToString(), conn);
                try
                {
                    conn.Open();
                    string gradename = cmdd.ExecuteScalar().ToString();
                    cmbgrade.Text = gradename;
                }
                catch (Exception)
                {
                    MessageBox.Show("错误");
    
                }
                finally
                {
                    conn.Close();
                }
            }
            //根据学号获得学生信息
            public void getstu()
            {
                SqlConnection con = readay();
                StringBuilder sql = new StringBuilder();
                sql.AppendFormat("select * from student where studentno={0}",stuid);
                SqlCommand cmd = new SqlCommand(sql.ToString(),con);
                try
                {
                    con.Open();
                    SqlDataReader br = cmd.ExecuteReader();
                    if (br!=null)
                    {
                        if (br.HasRows)
                        {
                            while (br.Read())
                            {
                                string id = br["studentno"].ToString();
                                string name = br["studentname"].ToString();
                                string gender = br["gender"].ToString();
                                string  gradeid=br["gradeid"].ToString();
                                string phone=br["phone"].ToString();
                                string address=br["address"].ToString();
                                string Birthday = br["Birthday"].ToString();
                                string Email = br["Email"].ToString();
                                txt_no.Text = id;
                                textnname.Text = name;
                                if (gender=="1")
                                {
                                    radionan.Checked = true;
                                }
                                else
                                {
                                    radionv.Checked = true;
                                }
                                getgradename(gradeid);
                                textphone.Text = phone;
                                textadders.Text = address;
                                combobirt.Text = Birthday;
                                textemail.Text = Email;
                            }
                            br.Close();
                        }
                        
                    }
                }
                catch (Exception)
                {
    
                    MessageBox.Show("错误");
                }
                finally 
                {
                    con.Close();
                }
               
            }
            private void addstudenng_Load(object sender, EventArgs e)
            {
                load();
                if (stuid!=0)
                {
                    getstu();
                    this.Text = "修改学生信息";
                    text_pwd.Enabled = false;
                    txtpwd.Enabled = false;
                }
            }
            public void setofinsertstu() 
            {
                string pwd = text_pwd.Text;
                string name = textnname.Text;
                string gender = string.Empty;
                if (radionan.Checked)
                {
                    gender = "1";
                }
                else
                {
                    gender = "0";
                }
                SqlConnection con = readay();
                string sql = "select gradeid from grade where gradename='" + cmbgrade.Text + "'";
                SqlCommand cmd = new SqlCommand(sql, con);
                int gid = 0;
                try
                {
                    con.Open();
                    gid = Convert.ToInt32(cmd.ExecuteScalar());
                }
                catch (Exception)
                {
                    MessageBox.Show("网络异常");
    
                }
                finally
                {
                    con.Close();
                }
                string phone = textphone.Text;
                string adders = textadders.Text;
                string birt = combobirt.Value.ToString();
                string emial = textemail.Text;
                string sql1 = "insert into student values('" + pwd + "','" + name + "','" + gender + "'," + gid + ",'" + phone + "','" + adders + "','" + birt + "','" + emial + "')";
                StringBuilder sql2 = new StringBuilder();
                sql2.AppendFormat("update student set studentname='{0}',gender='{1}',gradeid={2},phone='{3}',address='{4}',birthday='{5}',email='{6}' where studentno={7}",name,gender,gid,phone,adders,birt,emial,stuid);
                SqlConnection con1 = readay();
                SqlCommand cmd1 =null;
                if (stuid!=0)
                {
                    cmd1= new SqlCommand(sql2.ToString(), con1);
                }
                else
                {
                    cmd1 = new SqlCommand(sql1, con1);
                }
                try
                {
                    con1.Open();
                    int count = cmd1.ExecuteNonQuery();
                    if (count > 0)
                    {
                        MessageBox.Show("保存成功");
                    }
                    else
                    {
                        MessageBox.Show("保存失败");
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("网络异常");
    
                }
                finally
                {
                    con1.Close();
                }
            }
            private void buttonok_Click(object sender, EventArgs e)
            {
                if (stuid == 0)
                {
                    if (txtpwd.Text == text_pwd.Text && text_pwd.Text.Length >= 6)
                    {
                        if (textemail.Text.IndexOf('@') > 0)
                        {
                            setofinsertstu();
                        }
                        else
                        {
                            MessageBox.Show("邮箱格式不正确");
                        }
                    }
                    else
                    {
    
                        MessageBox.Show("密码不一致或密码长度小于6位");
                    }
                }
                else 
                {
                    setofinsertstu();
                }
            }
    
    
            private void no_Click(object sender, EventArgs e)
            {
                if (stuid!=0)
                {
                    this.Close();
                }
                else
                {
                    text_pwd.Text = null;
                    txtpwd.Text = null;
                    textphone.Text = null;
                    textadders.Text = null;
                    textnname.Text = null;
                    textemail.Text = null;
                    cmbgrade.Text = null;
                    combobirt.Text = null;
                    radionv.Checked = true;
                }
            }

    该窗体不仅可以添加学生记录还可以修改已存在的学生记录

    首先在load事件中判断用户是通过添加学生按钮还是修改学生信息按钮触发得该窗体.如果用户是通过修改学生信息按钮触发的该窗体则禁用密码框并执行实现修改功能的T-sql语句!

    两种不同的触发方式的界面如下:

                  修改学生信息窗体                                                    添加学生信息窗体

    image       image

     

    查询学生信息窗体

    查询学生信息的船体界面如下:

    image

    它是由按钮、文本框、List View控件和contextmenustrip控件组成它可以根据学生姓名查询学生信息

    其实现代码如下:

    public void databass()//在数据库中查询学生信息
            {
                string str = "data source=.;initial catalog=myschool;uid=sa;pwd=";
                SqlConnection con = new SqlConnection(str);
                string sql = "select studentno,studentname,gender,gradename from student inner join grade on(student.gradeid=grade.gradeid) where studentname like '%" + text_name.Text + "%'";
                SqlCommand cmd = new SqlCommand(sql, con);
                try
                {
                    con.Open();
                    SqlDataReader br = cmd.ExecuteReader();
                    if (br != null)
                    {
                        if (br.HasRows)
                        {
                            while (br.Read())
                            {
                                string stuno = Convert.ToString(br["studentno"]);
                                string stuname = Convert.ToString(br["studentname"]);
                                string gender = Convert.ToString(br["gender"]);
                                string gname = Convert.ToString(br["gradename"]);
                                ListViewItem lvitem = new ListViewItem(stuno);
                                lvitem.SubItems.Add(stuname);
                                lvitem.SubItems.Add(gender);
                                lvitem.SubItems.Add(gname);
                                lv_list.Items.Add(lvitem);
                            }
                            br.Close();
                        }
                    }
                }
                catch (Exception)
                {
    
                    MessageBox.Show("网络异常");
                }
                finally
                {
                    con.Close();
                }
            }
      
             //当用户单击查询按钮时清空list view空间                                                                                                    //  中的原有项病调用查询学生信息的方法
            private void btn_select_Click(object sender, EventArgs e)
    
            {
                lv_list.Items.Clear();
                databass();
            }
          //在load事件中将所有的学生信息添加到list view控件中
            private void lelectstu_Load(object sender, EventArgs e)
            {
                databass();
            }

     

    关于窗体

    关于窗体的界面如下:

    image

    关于窗体由标签、pictureboxd、timer和Imagelist控件组成以实现动态图像的效果其实现原理为在iamgelist控件中存放挑选好的图片在timer的Tick事件中将imagelist中的图片在picturebox控件中显示!其代码如下:

    int indext=0;//定义一个变量代表imagelist的下标项
            private void tt_Tick(object sender, EventArgs e)
            {
                if (indext<img_list.Images.Count-1)
                {
                    indext++;//如果indext的值小于imagelist的项的总数-1则indext自身加1
                }
                else
                {
                    indext = 0;//否则indext=0
                }
                ptb_image.Image=img_list.Images[indext];//将imagelist的indext项的图片在picturebox控件中显示
            }
  • 相关阅读:
    kali linux源大全
    kali2016.2(debian)快速安装mysql5.7.17
    hadoop java上传文件
    HDFS客户端的权限错误:Permission denied
    hadoop fs命令
    解决Unable to load native-hadoop library for your platform
    并查集。路径压缩 算法运用学习(一)
    HBase快速安装
    记一次zookeeper单机伪集群分布
    ZooKeeper使用命令大全
  • 原文地址:https://www.cnblogs.com/wangzheand/p/4997133.html
Copyright © 2020-2023  润新知