您的位置:首页 > 编程语言 > C#

C#

2017-01-08 23:22 134 查看
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.OleDb;

namespace login

{

    public partial class Form1 : Form

    {

        private OleDbDataAdapter adp;

        private DataSet myDataSet;

        private OleDbCommandBuilder ocb;

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            String Connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Company.mdb";

            OleDbConnection conn = new OleDbConnection(Connstring);

            String strSql = "SELECT * FROM Employer";

            adp = new OleDbDataAdapter(strSql,conn);

            myDataSet = new DataSet();

            adp.Fill(myDataSet, "Stud");

            ocb = new OleDbCommandBuilder(adp);

            this.dataGridView1.DataSource = myDataSet.Tables["Stud"];

        }

        private void button2_Click(object sender, EventArgs e)

        {

            foreach (DataGridViewRow decect in dataGridView1.SelectedRows)

            {

                if (MessageBox.Show("确定要更新数据吗", "更新数据", MessageBoxButtons.OKCancel) == DialogResult.OK)

                {

                    DataTable dt = myDataSet.Tables["Stud"];

                    int[] sel_rows = new int[dataGridView1.SelectedRows.Count];

                    //定义一个数组保存所选中的行               

                    for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)

                    {

                        sel_rows[i] = dataGridView1.SelectedRows[i].Index;

                    }

                    //根据数组选择所得到的行号删除数据表 

                    for (int i = 0; i < sel_rows.Length; i++)

                    {

                        dt.Rows[sel_rows[i]].Delete();

                    }

                    adp.Update(myDataSet, "Stud");

                }

                else {

                    MessageBox.Show("没有删除数据");

                }

            }

        }

        private void button3_Click(object sender, EventArgs e)

        {

            if (MessageBox.Show("确定要更新数据吗", "更新数据", MessageBoxButtons.OKCancel) == DialogResult.OK)

            {

                adp.Update(myDataSet, "Stud");

            }

            else {

                MessageBox.Show("没有更新数据!");

            }

        }

        private void button4_Click(object sender, EventArgs e)

        {

            if (MessageBox.Show("确定要插入数据吗", "插入数据", MessageBoxButtons.OKCancel) == DialogResult.OK)

            {

                this.dataGridView1.AllowUserToAddRows = false;

                dataGridView1.Rows.Add();

                adp.Update(myDataSet, "Stud");

            }

            else

            {

                MessageBox.Show("没有插入数据!");

            }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C#