zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    转 闭包简单理解
    mac 利用 sshpass 自动登录
    阮一峰 IaaS,PaaS,SaaS 的区别
    YMP运行初始化步骤
    强烈推荐 在线接口文档管理工具 小幺鸡 小团队可以省掉测试了
    springboot JSP 404
    并发测试 JavaDemo
    JS刷新当前页面的几种方法总结
    jquery checkbox勾选/取消勾选只能操作一次的诡异问题
    微信网页 第三方登录原理详解(转)
  • 原文地址:https://www.cnblogs.com/lxchutao/p/2225564.html
Copyright © 2011-2022 走看看