将datagridview内容直接更新到ACCESS数据库中

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

namespace DataGridViewTest
{
    public partial class Form1 : Form
    {
        private string m_ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\DataBase\MyDB.mdb";

        private DataSet ds;
        private OleDbDataAdapter dataAdapter;
       
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         
            OleDbConnection sqlCon = new OleDbConnection(m_ConStr);
            string sql = "select * from Users";
            dataAdapter = new OleDbDataAdapter(sql,sqlCon);
            sqlCon.Close();
            OleDbCommandBuilder cb = new OleDbCommandBuilder(dataAdapter);

            ds = new DataSet();
            dataAdapter.Fill(ds);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataAdapter.Update(ds);
        }

      
    }
}

你可能感兴趣的:(c#)