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

namespace WindowsApplication1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        DataBase MyDataBase = new DataBase();
        string sno, sname, sclass, mz, sbirthday;
        Int16 ssex = 0;
        /// <summary>
        /// 初始化页面，加载民族信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form4_Load(object sender, EventArgs e)
        {
            string sqlstring = "select * from mz";
            SqlDataReader dr= MyDataBase.GetDataReader(sqlstring);
            while (dr.Read())
            {
                comboBox1.Items.Add(dr[1].ToString());
            }
            clearpanel();
        }
        /// <summary>
        /// 获取界面内容信息
        /// </summary>
        private void GetMess()
        {
            sno = textBox3.Text;
            sname = textBox1.Text;
            sclass = textBox2.Text;
            if (radioButton1.Checked) ssex = 0;
            if (radioButton2.Checked) ssex = 1;
            sbirthday = dateTimePicker1.Value.ToLongDateString();
            mz = comboBox1.Text;
        }
        /// <summary>
        /// 清除界面，初始化界面
        /// </summary>
        private void clearpanel()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            comboBox1.SelectedIndex = -1;
            radioButton1.Checked = true;
            string someday="1990-01-01";
            dateTimePicker1.Value = Convert.ToDateTime(someday);
            textBox3.Focus();
        }
        /// <summary>
        /// 开始准备添加信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            GetMess();
            string insertsql = "insert into student(sno,sname,SEX,BIRTHDAY,CLASS,mz) values("+sno+",'"+sname+"',"+ssex+",'"+Convert.ToDateTime(sbirthday)+"','"+sclass+"','"+mz+"')";
            int i = MyDataBase.ExecuteSQL(insertsql);
            if (i < 0)
            {
                MessageBox.Show("数据库操作失败");
            }
            else
            {
                MessageBox.Show("数据库插入成功，已经影响"+i.ToString()+"行数据");
                button2.Enabled = true;
                button3.Enabled = true;
                clearpanel();
            }

        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            int i= MyDataBase.GetdataRow("select sno from student where sno=" + textBox3.Text);
            if (i == 0)
            {
                MessageBox.Show("无此数据");
            }
            else
            {
                GetMess();
                string updatesql = "update student set sname='" + sname + "',SEX='" + ssex + "',BIRTHDAY='" + Convert.ToDateTime(sbirthday) + "',CLASS='" + sclass + "',mz='" + mz + "' where sno="+sno;
                int k = MyDataBase.ExecuteSQL(updatesql);
                if (k < 0)
                {
                    MessageBox.Show("数据库操作失败");
                }
                else
                {
                    MessageBox.Show("数据库修改成功，已经影响" + i.ToString() + "行数据");
                    clearpanel();
                }
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            int i = MyDataBase.GetdataRow("select sno from student where sno=" + textBox3.Text);
            if (i == 0)
            {
                MessageBox.Show("无此数据");
            }
            else
            {
                GetMess();
                string deletesql = "delete from student where sno="+sno;
                int k = MyDataBase.ExecuteSQL(deletesql);
                if (k < 0)
                {
                    MessageBox.Show("数据库操作失败");
                }
                else
                {
                    MessageBox.Show("数据库删除成功，已经影响" + i.ToString() + "行数据");
                    clearpanel();
                }
            }
        }

    }
}