ECG在DbCore使用实体更新Access数据库的时候返回值为0时的问题解决方法

 问题描述:

在某些场景下,对于Access数据库,会出现如下更新问题:

myAccess.TSTENTITY entity = new LXChuTao.Entity.Common.Access.TSTENTITY();
DbCore dbCore = new DbCore(DatabaseType.Access, myAccess.BaseEntity.GetConnectionString());
int count = -1;
dbCore.Open();
entity.TSTID = 4; //这里该记录已存在
entity.TSTTXT = "测试4_1";
count = dbCore.Update(entity); //注意:对Access数据库这样不行, count 返回值为 0
dbCore.Close();

这里更新后返回受影响的行数count = 0,显然结果是不正确的,未更新数据。

 

解决方案:

此时可用entity.Update(dbCore)来解决,完整代码如下所示:

DbCore dbCore = new DbCore(DatabaseType.Access, myAccess.BaseEntity.GetConnectionString());

myAccess.TSTENTITY entity = new LXChuTao.Entity.Common.Access.TSTENTITY();

int count = -1;

dbCore.Open();

dbCore.BeginTransaction();

myAccess.employee emp = new LXChuTao.Entity.Common.Access.employee("1");

emp.lname = "UpdateDbcore";

count = emp.Update(dbCore);

 

myAccess.employee emp1 = new LXChuTao.Entity.Common.Access.employee(dbCore, "emp_id", "1");

myAccess.employee emp2 = new LXChuTao.Entity.Common.Access.employee("emp_id", "1");

string str = emp1.lname;//比较修改前后值

str = emp2.lname;//比较修改前后值

 

entity.TSTID = 4;

entity.TSTTXT = "测试4_1";

count = dbCore.Save(entity);

str = count.ToString();

 

entity.TSTTXT = "测试4_2";

count = dbCore.Update(entity);//注意:对Access数据库这样不行 count 返回值为 0

count = entity.Update(dbCore);//这样可以

 

dbCore.RollbackTransaction();

 

emp1 = new LXChuTao.Entity.Common.Access.employee(dbCore, "emp_id", "1");

emp2 = new LXChuTao.Entity.Common.Access.employee("emp_id", "1");

str = emp1.lname;//回滚事务后 再次比较值

str = emp2.lname;//回滚事务后 再次比较值

 

//dbCore.CommitTransaction();

dbCore.Close();

 

同entity.Update(dbCore)对应方法还有entity.UpdateAll(dbCore)/
ntity.UpdateEx(dbCore)/entity.Insert(dbCore)/entity.InsertALL(dbCore)/entity.InsertEx(dbCore)/entity.Save(dbCore)/entity.SaveAll(dbCore)/entity.SaveEx(dbCore)

    同样对于实体集,也相应的提供批量的保存方法,如:entitys.Save(dbCore)/
entitys.SaveAll(dbCore)/entitys.SaveEx(dbCore)

 

下载链接:

http://www.cnblogs.com/lxchutao/archive/2011/06/01/2065977.html

 

posted @ 2011-10-26 18:49  lxchutao  阅读(278)  评论(0编辑  收藏  举报