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 Form9 : Form
    {
        private SqlConnection mycon;
        private SqlDataAdapter myada;
        private SqlCommand mycomd;
        private SqlCommandBuilder mycbd;
        private DataSet myset;

        public Form9()
        {
            InitializeComponent();
            mycon = new SqlConnection("Data Source=(local);Initial Catalog=school;Persist Security Info=True;User ID=sa;Password= ");
            mycomd = new SqlCommand("select * from student",mycon);
            myada = new SqlDataAdapter();
            myada.SelectCommand = mycomd;
            mycbd = new SqlCommandBuilder(myada);
            myset = new DataSet();
            myada.TableMappings.Add("student","student");
            myada.TableMappings[0].ColumnMappings.Add("SNO", "学号");
            myada.TableMappings[0].ColumnMappings.Add("SNAME", "姓名");
            myada.TableMappings[0].ColumnMappings.Add("SEX", "性别");
            myada.TableMappings[0].ColumnMappings.Add("BIRTHDAY", "生日");
            myada.TableMappings[0].ColumnMappings.Add("CLASS", "班级");
            
        }
        /// <summary>
        /// 数据修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //将更改的数据更新到数据表里
                myada.Update(myset.Tables["student"].GetChanges());
                MessageBox.Show("数据库修改成功","成功信息");
                //DataTable接受更改,以便为下一次更改作准备
                myset.Tables["student"].AcceptChanges();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form9_Load(object sender, EventArgs e)
        {
            try
            {
                myada.Fill(myset, "student");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                mycon.Close();
            }
            dataGridView1.DataSource = myset.Tables["student"].DefaultView;
        }
        /// <summary>
        /// 数据删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除当前行数据？", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                try
                {
                    //从DataTable中删除当前选中的行
                    myset.Tables[0].Rows[dataGridView1.CurrentRow.Index].Delete();
                    //将更改的数据更新到数据表里
                    myada.Update(myset.Tables[0].GetChanges());
                    MessageBox.Show("数据删除成功！");
                    //DataTable接受更改,以便为下一次更改作准备
                    myset.Tables[0].AcceptChanges();
                }

                catch (SqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            else
            {
                //取消对DataTable的更改
                myset.Tables[0].RejectChanges();
            }
        }
    }
}