C#

来源:互联网 发布:听小说的软件 编辑:程序博客网 时间:2024/06/14 01:24
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("没有插入数据!");
            }
        }
    }
}
0 0
原创粉丝点击