studenteditform.cs

来自「一个不错的文档」· CS 代码 · 共 337 行

CS
337
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySchoolPrj.Entity;
using MySchoolPrj.DAO;
using System.Data.SqlClient;
using System.Collections;

namespace MySchoolPrj.MainForm
{
    public partial class StudentEditForm : Form
    {
        private State state;

        public State State
        {
            get { return state; }
            set { state = value; }
        }
        private Student stu;

        internal Student Stu
        {
            get { return stu; }
            set { stu = value; }
        }
        public StudentEditForm()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //初始化界面,将获取的班级信息添加到下拉框
        public void getClass()
        {
            try
            {
                DBHelper.con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DBHelper.con;
                cmd.CommandType = CommandType.Text;
                string sql = string.Format("select * from classInfo");
                cmd.CommandText = sql;
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Class ci = new Class();
                    ci.ClassID = reader["classID"].ToString();
                    cmbClass.Items.Add(ci.ClassID);
                }
                cmbClass.DropDownStyle = ComboBoxStyle.DropDownList;
                cmbClass.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.con.Close();
            }
        }

        //将对象的数据显示到界面
        public void showData()
        {
            txtStuNo.Text = stu.StuID;
            txtStuName.Text = stu.StuName;
            if (stu.StuSex == "男")
                rbtnMan.Checked = true;
            else
                rbtnWoman.Checked = true;
            dtpBirthday.Value = stu.StuBirthday;
            txtPhone.Text = stu.StuPhone+"";
            txtAddress.Text = stu.StuAddress;
            dtpInDate.Value = Convert.ToDateTime(stu.StuInDate);
            cmbClass.SelectedItem = stu.ClassID;
            
        }

        //获取界面数据
        public Student getInteface()
        {
            Student s = new Student();
            s.StuID = txtStuNo.Text;
            s.StuName = txtStuName.Text;
            if (rbtnMan.Checked)
            {
                s.StuSex = "男";
            }
            else
                s.StuSex = "女";
            s.StuBirthday = dtpBirthday.Value;
            s.StuPhone = txtPhone.Text.ToString();
            s.StuAddress = txtAddress.Text;
            s.StuInDate = dtpInDate.Value.ToShortDateString();
            s.ClassID = cmbClass.SelectedItem.ToString();
            s.Memo = txtMemo.Text;
            return s;
        }

        //将学生信息提交到数据库
        public void addStu(Student s)
        {
            try
            {
                DBHelper.con.Close();
                DBHelper.con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DBHelper.con;
                cmd.CommandType = CommandType.Text;
                string sql = string.Format("insert into student values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')",
                    s.StuID,s.StuName, s.StuSex, s.StuBirthday, s.ClassID, s.StuPhone, s.StuInDate, s.StuAddress, s.Memo);
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.con.Close();
            }
        }

        //修改学生信息入库
        public void UpdateStu(Student s)
        {
            try
            {
                DBHelper.con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DBHelper.con;
                cmd.CommandType = CommandType.Text;
                string sql = string.Format("update student set stuName='{0}',StuSex='{1}',StuBirthday='{2}'," +
                    "ClassID='{3}',StuPhone='{4}',StuInDate='{5}',StuAddress='{6}',Memo='{7}' where stuID='{8}'", s.StuName, s.StuSex, s.StuBirthday, s.ClassID, s.StuPhone, s.StuInDate, s.StuAddress, s.Memo,stu.StuID);
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.con.Close();
            }
        }

        private void StudentEditForm_Load(object sender, EventArgs e)
        {
            rbtnMan.Checked = true;
            getClass();
            if (state == State.dsUpdate)
            {
                btnOK.Enabled = true;
                showData();
                txtStuNo.ReadOnly = true;
                if (UserHelper.grade == 1)
                {
                    cmbClass.Enabled = false;
                }
                else
                {
                    cmbClass.Enabled = true;
                }
            }
        }

        public bool NotNull()
            {
            DBHelper.con.Open();
            SqlCommand cmd=new SqlCommand();
            cmd.Connection=DBHelper.con;
            cmd.CommandType=CommandType.Text;
            string sql=string.Format("Select stuId from student where stuId='{0}'",txtStuNo.Text);
            cmd.CommandText=sql;
            SqlDataReader reader=cmd.ExecuteReader();
            
             if(reader.HasRows)
            {
                MessageBox.Show("对不起,已有此学生,请重新输入","输入提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                
                DBHelper.con.Close();
                return true;
            }
            DBHelper.con.Close();
            return false;
    }
        //判断该班的学生人数是否超过班级人数
        //public bool
        private void btnOK_Click(object sender, EventArgs e)
        {
            
              
                if (state == State.dsAdd)
                {
                    if (txtStuNo.Text==""||txtStuName.Text == "" ||txtPhone.Text == "" || txtAddress.Text == "")
                    {
                        MessageBox.Show("对不起,您的信息没有填写完整!", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    
                    if (NotNull())
                    {
                        txtStuNo.Focus();
                    }
                    else
                    {
                        addStu(getInteface());
                        this.Close();
                    }
                }
                else
                {
                    if (txtStuNo.Text.Length <1 || txtStuName.Text == "" ||txtPhone.Text == "" || txtAddress.Text == "")
                    {
                        MessageBox.Show("对不起,您的信息没有填写完整!", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        UpdateStu(getInteface());
                        this.Close();
                    }
                }
            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtStuNo.Text = "";
            txtStuName.Text = "";
            txtPhone.Text = "";
            txtAddress.Text = "";
            txtMemo.Text = "";
        }
        //自动产生学号
        ArrayList array = new ArrayList();
        public void autoCreateStuId()
        {
            array.Clear();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = DBHelper.con;
            string sql = "select stuid from student where classid='" + cmbClass.SelectedItem.ToString()+
                "'";
            cmd.CommandText = sql;
            DBHelper.con.Close();
            DBHelper.con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                array.Add(dr[0].ToString());
            }
            dr.Close();
            DBHelper.con.Close();
            if (array.Count == 0)
            {
                txtStuNo.Text = cmbClass.SelectedItem.ToString() + "1";
            }
            else
            {
                for (int i = 0; i < array.Count; i++)
                {
                    int count = cmbClass.SelectedItem.ToString().Length;
                    int a = Convert.ToInt32(array[i].ToString().Substring(count)) + 1;
                    if (i == array.Count - 1)
                    {
                        txtStuNo.Text = cmbClass.SelectedItem.ToString() + a;
                        break;
                    }
                    else
                    {
                        int b = Convert.ToInt32(array[i + 1].ToString().Substring(count));
                        if (a != b)
                        {
                            txtStuNo.Text = cmbClass.SelectedItem.ToString() + a;
                            break;
                        }
                    }
                }
            }
        }

        private void cmbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            autoCreateStuId();
            judgeClassCount();
        }
        //判读该班实际人数是否超过班级规定人数
        public void judgeClassCount() {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = DBHelper.con;
            string sql = "select count(*) from student where classid='" + cmbClass.SelectedItem.ToString() +
                "'";
            cmd.CommandText = sql;
            DBHelper.con.Open();
            int count = (int)cmd.ExecuteScalar();
            sql = "select studentcount from classInfo where classid='"+cmbClass.SelectedItem.ToString()+"'";
            cmd.CommandText = sql;
            int ccount = (int)cmd.ExecuteScalar();
            DBHelper.con.Close();
            if (count == ccount)
            {
                txtStuNo.ReadOnly = true;
                txtStuNo.Text = "该班人数已满!";
                btnOK.Enabled = false;
            }
            else {
                txtStuNo.ReadOnly = false;
                btnOK.Enabled = true;
            }
        }
        //设置电话只能为数字
        private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch = e.KeyChar;
            
            if ((Keys)ch != Keys.Back)
            {
                if (ch < '0' || ch > '9')
                    e.Handled = true;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?