自动更新Access数据库public partial class Form1 : Form
    {
        public OleDbConnection conn = null;
        public DataSet ds = null;
        public OleDbDataAdapter adp = null;
        public OleDbCommand command = null;
        public OleDbCommandBuilder bu = null;        public Form1()
        {
            InitializeComponent();            bindData();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            
            // TODO: 这行代码将数据加载到表“dataSet.Structure”中。您可以根据需要移动或移除它。
            //this.structureTableAdapter.Fill(this.dataSet.Structure);
            treeList1.DataSource = ds.Tables[0];
            treeList1.KeyFieldName = "ID";
            treeList1.ParentFieldName = "ParentID";
        }        public void bindData()
        {
            treeList1.OptionsView.ShowCheckBoxes = true;//是否显示CheckBox
            conn = new OleDbConnection(ConfigurationManager.AppSettings["con"]);
            conn.Open();            //treeList1.OptionsBehavior.AllowIndeterminateCheckState = true; //设置节点是否有中间状态,即一部分子节点选中,一部分子节点没有选中            command = new OleDbCommand();
            command.Connection = conn;
            command.CommandText = "select * from  Structure";
            command.ExecuteNonQuery();            adp = new OleDbDataAdapter(command);
            bu = new OleDbCommandBuilder(adp);
            adp.SelectCommand = command;
            adp.DeleteCommand = bu.GetDeleteCommand();
            adp.InsertCommand = bu.GetInsertCommand();
            adp.UpdateCommand = bu.GetUpdateCommand();            ds = new DataSet();
            adp.Fill(ds);  //listView可以显示出来数据库内容
        }            private void treeList1_CellValueChanged(object sender,  DevExpress.XtraTreeList.CellValueChangedEventArgs e)
        {
            //ds.AcceptChanges();
            try
            {              
                //if (ds.HasChanges())
                //{ 
                adp.Update(ds);
                ds.AcceptChanges(); //ds中内容已经变化,可执行完后数据库值不变化
                    
                //}
            }
            catch
            {            }
            finally {
                conn.Close();
            }        }
    }

解决方案 »

  1.   

    因为你没有为OleDbDataAdapter 设置更新逻辑,例如插入、删除、修改的逻辑,需要手工设置
    OleDbDataAdapter.InsertCommand 
    OleDbDataAdapter.UpdateCommand 
    OleDbDataAdapter.DeleteCommand 或者这样让 自动生成更新逻辑:
    OleDbCommandBuilder builder=new OleDbCommandBuilder(OleDbDataAdapter);
      

  2.   

    在TreeList控件上绑定数据源后,在TreeList上改变了数据后,DataSet数据集也改变了,OleDbDataAdapter.Update(ds);
    ds.AcceptChanges();执行后 ,数据库信息还是没有改变为什么?
      

  3.   

    DataSet数据集是本地的,数据库中的内容并没有更新。ds.AcceptChanges()调用后,会判断对应的dataAdapter是否有更新逻辑,如果有就会调用更新逻辑去更新数据到数据库,你没有看到数据库的信息被修改,是因为你没有设置更新逻辑。
      

  4.   

    我仔细看了你的代码,你有设置更新逻辑:  adp = new OleDbDataAdapter(command);
      bu = new OleDbCommandBuilder(adp);
      adp.SelectCommand = command;
      adp.DeleteCommand = bu.GetDeleteCommand();
      adp.InsertCommand = bu.GetInsertCommand();
      adp.UpdateCommand = bu.GetUpdateCommand();这句更新有出现异常吗?
    adp.Update(ds);