日期:2014-05-19  浏览次数:20882 次

OleDbDataAdapter.update无法更新到access数据库,搞不定,高分求教,速度结帖
_oleDbConnection   =   global.GetConn();
_oleDbDataAdapter   =   new   OleDbDataAdapter( "select   *   from   brand ",_oleDbConnection);
_oleDbConnection.Open();
BrandDataSet   =   new   DataSet();
_oleDbDataAdapter.Fill(BrandDataSet);
_oleDbConnection.Close();
dataGrid1.DataSource   =   BrandDataSet.Tables[0];

//以上是读取,没问题

dataGrid1增加修改一些数据后执行以下

BrandDataSet.AcceptChanges();
BrandDataSet.Tables[0].AcceptChanges();
OleDbCommandBuilder   bu   =   new   OleDbCommandBuilder(   _oleDbDataAdapter   );
_oleDbDataAdapter.InsertCommand   =   bu.GetInsertCommand();
_oleDbDataAdapter.UpdateCommand   =   bu.GetUpdateCommand();
_oleDbDataAdapter.Update(   BrandDataSet,   BrandDataSet.Tables[0].TableName   );
this.Close();

//dataGrid1修改之后,dataset里面的数据已经修改了,但问题是怎么也无法提交修改到数据库,也不报错,搞一晌午没搞定,实在不行了,求仙人指教
难道是数据库权限的问题?

------解决方案--------------------
BrandDataSet.AcceptChanges();
BrandDataSet.Tables[0].AcceptChanges();
OleDbCommandBuilder bu = new OleDbCommandBuilder( _oleDbDataAdapter );
_oleDbDataAdapter.InsertCommand = bu.GetInsertCommand();
_oleDbDataAdapter.UpdateCommand = bu.GetUpdateCommand();
_oleDbDataAdapter.Update( BrandDataSet, BrandDataSet.Tables[0].TableName );
this.Close();
====================================================================

OleDbCommandBuilder bu = new OleDbCommandBuilder( _oleDbDataAdapter );
_oleDbDataAdapter.InsertCommand = bu.GetInsertCommand();
_oleDbDataAdapter.UpdateCommand = bu.GetUpdateCommand();
_oleDbDataAdapter.Update( BrandDataSet, BrandDataSet.Tables[0].TableName );
BrandDataSet.AcceptChanges();
------解决方案--------------------
winform 还是webform

winform的话 看看是否是 你的数据加载的方法在你的 更新前被运行了

webform 看看 数据 绑定的 地方
加上

if(!IsPostBack)
{
//数据绑定
}
------解决方案--------------------
Update是用来提交ds中标示为有改动的数据 所以要先update再调用AcceptChanges
------解决方案--------------------
DataTable.AcceptChanges 方法

调用 AcceptChanges 时,任何仍处于编辑模式的 DataRow 对象将成功结束其编辑。DataRowState 也发生更改:所有 Added 和 Modified 行都变为 Unchanged,Deleted 行则被移除。

在您尝试使用 DbDataAdapter.Update 方法更新 DataSet 之后,通常会对 DataTable 调用 AcceptChanges 方法。

------解决方案--------------------
不要在Update之前调用AcceptChanges方法。否则Update不会更新任何数据!
------解决方案--------------------
_oleDbDataAdapter.Update( BrandDataSet.Tables[0].GetChanges() );

------解决方案--------------------
先Update再AcceptChanges