C#第二章上机题

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 编辑学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.user = "徐小倩";
            f.MdiParent = this;
            f.Show();
        }



       
    }
}










using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {

        private string connString = @"Data Source=.;Initial Catalog=MySchool;Integrated Security=True";

        public Form2()
        {
            InitializeComponent();
        }

        public string user = string.Empty;
        private void Form2_Load(object sender, EventArgs e)
        {
            label1.Text = user;
            
            comboBox1.Items.Add("请选择");
            comboBox1.SelectedIndex = 0;
            BindGrade();

        }




         DBHelper db = new DBHelper();
        public string CAPTION = "提示输入";

        public bool BindGrade()
        {
           
            try
            {

                string sql = "SELECT * FROM Grade";
                db.OpenConnection();
                SqlCommand comm = new SqlCommand(sql,db.Connection);
              
                SqlDataReader myReader = comm.ExecuteReader();
                while (myReader.Read())
                {
                    string gradeName = myReader["GradeName"].ToString();
                    this.comboBox1.Items.Add(gradeName);
                }
                myReader.Close();
                return true;
            }
            catch (Exception)
            {

                MessageBox.Show("系统发生错误!", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            finally
            {
                db.CloseConnection();
            }
        }

        public bool CheckInput()
        {
            if (this.textBox3.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入姓名", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBox3.Focus();      
                return false;
            }else if(this.comboBox1.Text.Trim().Equals("请选择"))
            {
                MessageBox.Show("请选择年级", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.comboBox1.Focus();
                return false;
            }
            else if (this.textBox2.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入密码", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBox2.Focus();
                return false;
            }
            else if (this.textBox6.Text.Trim().Equals(string.Empty) )
            {
                MessageBox.Show("请输入Email", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBox6.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }


        public bool InsertStudent()
        {
            
            DateTime date = this.dateTimePicker1.Value;
            string birthday = string.Format("{0}-{1}-{2}", date.Year, date.Month, date.Day);
            int gender = 0;
            if (this.radioButton1.Text.Equals("女"))
            {
                gender = 0;
            }
            else
            {
                gender = 1;
            }
           
                string sqlName = String.Format("SELECT GradeId FROM Grade WHERE GradeName='{0}'", this.comboBox1.Text);
                SqlCommand comm = new SqlCommand(sqlName, db.Connection);
                db.OpenConnection();
                int gradeId = (int)comm.ExecuteScalar();

                try
                {
                   
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("INSERT INTO Student (LoginPwd,StudentName,Gender,GradeId,Phone,Address,Birthday,Email)");
                    sb.AppendFormat("values ('{0}' ", this.textBox2.Text);
                   
                   sb.AppendFormat("  ,'{0}',{1}                                                                                                                                                                                                                            ,{2}", this.textBox3.Text,
                gender, gradeId);
                   MessageBox.Show(sb.ToString());
                    sb.AppendFormat(" ,'{0}','{1}','{2}','{3}')", this.textBox4.Text, this.textBox5.Text, birthday, this.textBox6.Text);
                comm = new SqlCommand(sb.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.textBox1.Text = studentNo.ToString();
                    return true;

                }
                return true;
            }
            catch (Exception e)
            {
                throw;
                return false;
            }
            finally
            {
                db.CloseConnection();
            }
        }



        private void button1_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                InsertStudent();
            }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            
            

        }

        private void textBox6_TextChanged_1(object sender, EventArgs e)
        {
            int i = this.textBox6.Text.IndexOf("@");
            if (i == -1)
            {
                MessageBox.Show("请Email格式错误", CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBox6.Focus();

            }


        }

    }
}

你可能感兴趣的:(c#)