自动更新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();
} }
}
OleDbDataAdapter.InsertCommand
OleDbDataAdapter.UpdateCommand
OleDbDataAdapter.DeleteCommand 或者这样让 自动生成更新逻辑:
OleDbCommandBuilder builder=new OleDbCommandBuilder(OleDbDataAdapter);
ds.AcceptChanges();执行后 ,数据库信息还是没有改变为什么?
bu = new OleDbCommandBuilder(adp);
adp.SelectCommand = command;
adp.DeleteCommand = bu.GetDeleteCommand();
adp.InsertCommand = bu.GetInsertCommand();
adp.UpdateCommand = bu.GetUpdateCommand();这句更新有出现异常吗?
adp.Update(ds);