C#用DataGridView对Access的浏览读取和更新改写 本人菜鸟,分享一下自己的学习经验,谢谢,欢迎评论,我随时会答复

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;
using System.IO;

namespace WindowsFormsAppdata
{
    public partial class Form1 : Form
    {
        DataTable dt;
        OleDbCommandBuilder cmd;
        OleDbDataAdapter oda;

        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)//这里是读取数据库的按钮。不能修改里面的参数。
        {

            string con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox1.Text.Trim() + " ";////定义连接字符串的路径。
            OleDbConnection Con = new OleDbConnection(con);//连接数据库的接头。
            Con.Open();                                    //接头打开的意思。
            oda = new OleDbDataAdapter("Select * from " + comboBox1.Text.Trim() + " ", con);//适配器,是数据源和内存数据之间的桥梁。
            dt = new DataTable(); //内存中的表,可以单独使用,常用于与适配器一起使用,通过适配器将数据从数据库中提取出来,并填充到数据集中
            cmd =new OleDbCommandBuilder(oda);
            if (button1.Text == "读取")
            {
                oda.Fill(dt);               //数据填充到内存之中,等待使用。
                dataGridView1.DataSource = dt;    //在控件中把填充在内存中的信息读取出来。
                if (dataGridView1.DataSource != null)
                {
                    button1.Text = "更新数据";
                    label1.Text = "读取成功,可以尝试对其修改并更新数据";
                }
                Con.Close();
            }
            else if (button1.Text == "更新数据") 
            {
                 DialogResult se = MessageBox.Show("你真的要更新数据吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (se == DialogResult.OK)
                {
                    try
                    {
                        dt = (DataTable)dataGridView1.DataSource;//将dataGridView强制装换为datatable表里
                        cmd.GetUpdateCommand();//执行更新指令;
                        oda.Update(dt);//更新datatable;
                        //  dt.AcceptChanges();
                        // dt.Clear();
                        //oda.Fill(dt); 
                        MessageBox.Show("已经成功更新数据");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    return;
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();     //开始自己浏览需要的文件
            ofd.Filter = "所有文件|*.mdb;*.accdb;";        //过滤器,过滤得到自己想要的文件,一般格式为“XXX|*文件后缀;*文件后缀;”;
            ofd.RestoreDirectory = true;                   //记录上次打开的子目录。
            if (ofd.ShowDialog() == DialogResult.OK)       //判断是否选择好文件,
            {
                button1.Text = "读取";
                string filename = ofd.FileName;
                textBox1.Text = filename;
                string con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox1.Text + " ";//定义连接字符,
                OleDbConnection Con = new OleDbConnection(con);//初始化连接的接口。
                try
                {
                    if (Con.State == ConnectionState.Closed)//判断连接有没有打开,如果没打开下面就打开,
                        Con.Open();
                    comboBox1.Items.Clear();                //每次都清理下来列表框的信息。
                    dt = Con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                    foreach (DataRow row in dt.Rows)
                    {
                        comboBox1.Items.Add(row["TABLE_NAME"].ToString());
                    }
                    comboBox1.SelectedIndex = 0;


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (Con.State == ConnectionState.Open) Con.Close(); Con.Dispose();
                }

            }

        }
    }
}                                          

猜你喜欢

转载自blog.csdn.net/weixin_42229330/article/details/80485271