当前位置:编程学习 > C#/ASP.NET >>

ADO.NET如何更新ACCESS数据库?

ADO.NET如何更新ACCESS数据库?我用了好几种方法都更新不了。求可以正常运行的代码 --------------------编程问答-------------------- 贴代码和更新不了的错误信息 --------------------编程问答--------------------
public partial class Form1 : Form
    {
        private OleDbConnection connection = null;
        private OleDbCommand selectCommand = null;
        private OleDbDataAdapter dataAdapter = null;
        private string connectionString = 
            @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\DB\Customers.mdb;Persist Security Info=True";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            connection = new OleDbConnection(connectionString);
            selectCommand = connection.CreateCommand();
            selectCommand.CommandText = "SELECT CustomerID, CompanyName, ContactName, Address, Phone FROM Customers";
            dataAdapter = new OleDbDataAdapter();
            dataAdapter.SelectCommand = selectCommand;
            DataTable data = new DataTable();
            dataAdapter.Fill(data);
            this.dataGridView1.DataSource = data;

            OleDbCommandBuilder builder = new OleDbCommandBuilder(dataAdapter);
            dataAdapter.UpdateCommand = builder.GetUpdateCommand();
            dataAdapter.InsertCommand = builder.GetInsertCommand();
            dataAdapter.DeleteCommand = builder.GetDeleteCommand();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable data = (DataTable)this.dataGridView1.DataSource;
            DataTable changedData = data.GetChanges();

            if (changedData != null)
            {
                dataAdapter.Update(changedData);
                data.AcceptChanges();
            }
        }
    }
--------------------编程问答--------------------
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,