Dapepr的Update和Delete
Dapper入门
Dapper查询
上两篇文章我们介绍Dapper中添加和查询。本篇文章我们继续讲解修改和删除....如果本篇文章看不懂,请看阅读上两篇Dapper系列相关文章
1、修改Update
1.1单表操作
/// <summary> /// 单、多表修改 /// </summary> /// <returns></returns> public bool update() { #region 单表修改 Department dep = new Department(); dep.ID = new Guid("00205a48-b1fb-4e0a-94f8-05b65e3ed389"); dep.Name = "开发部门-修改"; string sql = @" update department set Name=@Name where ID=@ID"; return DapperDBContext.Execute(sql, dep) > 0; #endregion }
修改成功:
1.2多表同时修改--事务,该事务在Dapper第一篇章节有封装的方法,这里直接调用
/// <summary> /// 多表修改-事务 /// </summary> /// <returns></returns> public bool update() { #region 多表修改 var param = new List<Tuple<string, object>>() { new Tuple<string,object>(@" Update department Set Name=@Name where ID=@ID; ",new { Name = "WebAPI研发中心", ID =new Guid("00205a48-b1fb-4e0a-94f8-05b65e3ed389") }), new Tuple<string,object>(@" Update authresources Set IsDel=@IsDel where ID=@ID ; ",new { IsDel = 1, ID =new Guid("02658c43-546e-4e62-8e88-34816864b798") })}; return DapperDBContext.ExecuteTransaction(param).Item1; #endregion }
修改成功:
2、修改Delete
2.1单表删除
/// <summary> /// 单表删除 /// </summary> /// <returns></returns> public bool Delete() { #region string sql = @"delete from Departrment where ID = @ID;";return DapperDBContext.Execute(sql, new { ID = "121313" }) > 0; #endregion }
2.2多表删除
/// <summary> /// 多表删除-事务 /// </summary> /// <returns></returns> public bool Delete() { //同时删除多张表 事务删除:原子性,统一性,安全,同步 #region var param = new List<Tuple<string, object>>() { new Tuple<string,object>(@" delete from department where ID = @ID;",new { ID=new Guid("02658c43-546e-4e62-8e88-34816864b798"), }), new Tuple<string,object>(@" delete from AuthResources where ID =@ID;",new { ID=new Guid("02658c43-546e-4e62-8e88-34816864b798"), }) }; var result = DapperDBContext.ExecuteTransaction(param); return result.Item1; #endregion }
Dapper 的讲解我们就到此告一段落,三篇博客足以满足我们在项目中的使用。
欢迎进入Dapper博客学习:
- 博主是利用读书、参考、引用、复制和粘贴等多种方式打造成自己的纯镀 24k 文章,请原谅博主成为一个无耻的文档搬运工!
- 小弟刚迈入博客编写,文中如有不对,欢迎板砖扶正,希望对你有所帮助。