实现完整版的管理员主窗体和学生主窗体

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

        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {            
         }

        public bool DBHelper() {
           

            DBHelper db = new DBHelper();

            try
            {
                string sql = "Select*from[Grade]";

                db.OpenConnection();

                SqlCommand com = new SqlCommand(sql, db.Connection);

                SqlDataReader reader = com.ExecuteReader();
                this.comboBox1.Items.Add("请选择");
                this.comboBox1.SelectedIndex = 0;

                while (reader.Read())
                {
                    string gradename = reader["GradeName"].ToString();
                    comboBox1.Items.Add(gradename);
                }
                reader.Close();
                return true;
            }
            catch (Exception ex)
            {

                MessageBox.Show("系统发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            finally {
                db.CloseConnection();
            }


        }

        private void Form2_Load(object sender, EventArgs e)//调用上面的方法 
        {
            DBHelper();
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
              
        }

      public void insertStudent() {
         bool success = false;

         string pwd = this.textBox2.Text.Trim();//密码
         string name = this.textBox4.Text.Trim();//姓名

         int gender = 0;//获取性别
         if (this.radioButton1.Checked) {
             gender = 0;
         }
         else {
             gender = 1;
         }
          int genderId = (int)gender;
          string grade=this.comboBox1.Text.Trim();//年级
          string phone=this.textBox5.Text.Trim();//电话
          string address=this.textBox6.Text.Trim();//地址
          DateTime date = this.dateTimePicker1.Value;//出生日期
          string birthday=string.Format("{0}-{1}-{2}",date.Year,date.Month,date.Day);//生日
          string email=this.textBox7.Text.Trim();//邮件

          DBHelper db = new DBHelper();

         try 
	{	        
		 string sqlName = String.Format("select gradeId from grade where gradeName='{0}'",grade);
          SqlCommand com = new SqlCommand(sqlName,db.Connection);//创建Command对象

          db.OpenConnection();

          int gradeId = (int)com.ExecuteScalar();//获得年级编号

          StringBuilder Sql = new StringBuilder();

          Sql.AppendLine(@"insert into [Student],[StudentName],[Gender],[GradeId],[Phone],[Address],
              [Birthday],[Email]");
          Sql.AppendFormat("VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",pwd,name,
              genderId,gradeId,phone,address,birthday,email);

           com = new SqlCommand(Sql.ToString(), db.Connection);//创建Command对象

          db.OpenConnection();

          int result = com.ExecuteNonQuery();

          if(result==1){

              string SqlNo = "select @@IDENTITY from [student]";
              com.CommandText = SqlNo;
              int studentNo = Convert.ToInt32(com.ExecuteScalar());//学号
              this.textBox1.Text = studentNo.ToString();

              success = true;

	}
         }
	catch (Exception ex)
	{		
		success=false;
	}
          
    finally{
         db.CloseConnection();
         }
          
          
          }
          

          

        }
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值