MySchool--窗口(c#)

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } private void 编辑学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.BindGrade();
            f.MdiParent = this;
            f.Show();
        }

 public partial class Form2 : Form
    {  
        public Form2()
        {
            InitializeComponent();
        }

       
        public bool BindGrade()
        {
            DBHelper db = new DBHelper();
            try
            {
                string sql = "select * from grade";
                db.OpenConnection();
                SqlCommand comm = new SqlCommand(sql, db.Connection);
                SqlDataReader reader = comm.ExecuteReader();
       nianji.Items.Add("请选择");
            nianji.SelectedIndex =0;
                while (reader.Read())
                {
                    string gradeName = reader["gradename"].ToString();
                    nianji.Items.Add(gradeName);
             
                }
                reader.Close();
                return true;
            }
            catch (Exception )
            {

                MessageBox.Show("系统发生错误!", "调试", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;

            }
            finally {

                db.CloseConnection();
           }

        }

        private void nianji_SelectedIndexChanged(object sender, EventArgs e)
        {
      
         
        }
        public bool CheckInput() { 
        if(mima.Text.Trim().Equals(string.Empty)){

            MessageBox.Show("请输入密码","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            this.mima.Focus();
            return false;

        }else if(xingming.Text.Trim().Equals(string.Empty)){

            MessageBox.Show("请输入姓名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            this.xingming.Focus();
            return false;
        }else if(nianji.Text.Trim().Equals(string.Empty)){
            MessageBox.Show("请选择年级", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            this.nianji.Focus();
            return false;
        }
        else if (email.Text.Trim().Equals(string.Empty))
        {
            int i = this.email.Text.IndexOf('@');
            if (i == -1)
            {
MessageBox.Show("请输入正确邮箱", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            this.email.Focus();
            return false;
            }
            else {
                return true;
            }
            
        }
        else {
            return true;
        
        }
        
        
        
        
        }

        private void baocun_Click(object sender, EventArgs e)
        {
            if (CheckInput() && InsertStudent())
            {
                
                    MessageBox.Show("登陆成功","提示",MessageBoxButtons.OK,MessageBoxIcon.None);
                
                }
            }
        public bool InsertStudent() { 
            bool success = false;
            string phone = this.Text.Trim();
            int gender =0;
            if (this.nan.Checked)
            {
                gender =1;

            }
            
            string pwd = this.mima.Text.Trim();
            string gendername =this.nianji.Text.Trim();
            string address = this.dizhi.Text.Trim();

            string name = this.xingming.Text.Trim();
            DBHelper db = new DBHelper();
            DateTime date = this.chushengriqi.Value;
            string br = string.Format("{0}-{1}-{2}",date.Year,date.Month,date.Day);
            try
            {
                string sqlName = string.Format("select gradeid from grade where gradeName='{0}'", gendername);
                SqlCommand comm = new SqlCommand(sqlName, db.Connection);
                db.OpenConnection();
                int gradeId = (int)comm.ExecuteScalar();
                StringBuilder sql = new StringBuilder();
                sql.AppendLine(" insert into student (loginpwd,studentname,gender,gradeid ,phone,address ,birthday ,email) ");
                sql.AppendFormat(" values('{0}','{1}',{2},{3},'{4}','{5}','{6}','{7}')", pwd, name,gender, gradeId, phone, address, br, email);
                comm = new SqlCommand(sql.ToString(), db.Connection);
                db.OpenConnection();
                int result = comm.ExecuteNonQuery();
                if (result == 1)
                {
                    string sqlNo = "select @@IDENTITY FROM student";
                    comm.CommandText = sqlNo;
                    int studentNo = Convert.ToInt32(comm.ExecuteScalar());
                    this.xuehao.Text = studentNo.ToString();
                    success = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                success = false;
            }
            finally {

                db.CloseConnection();
            }



            return success;
        } 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值