.NET之Dapper框架运用
Dapper框架
1.项目引用Dapper的Nuget程序包;
2.配置链接类
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Dapper
{
public class DapperConn
{
public static IDbConnection GetConnection()
{
string connStr = ConfigurationManager.AppSettings["conn"];
return new SqlConnection(connStr);
}
}
}
3.配置相应表的实体对象
目前是一个用户表和一个用户登录日志表为例:
用户表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Dapper
{
public class UserModel
{
public Int32 Id { get; set; } public String Key { get; set; } public String Value { get; set; } }
}
用户登录日志表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Dapper
{
public class UserLoginLog
{
public Int32 Id { get; set; } public Int32 UserId { get; set; } public DateTime CreateTime { get; set; } }
}
4.通过实体对数据库操作
(包含基本的:增删改查及事务提交操作)
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Dapper
{
public class DB
{ public int Add(UserModel model)
{
using (var conn = DapperConn.GetConnection())
{
string sql = "Insert into User (Key,Value) Value (@Key,@Value)";
return conn.Execute(sql, model);
}
} public int Add1(UserModel model)
{
using (var conn = DapperConn.GetConnection())
{
DynamicParameters dp = new DynamicParameters();
dp.Add("@Key", model.Key);
dp.Add("@Value", model.Value);
return conn.Execute("User", dp, null, null, CommandType.TableDirect);
}
} public int Update(UserModel model)
{
using (var conn = DapperConn.GetConnection())
{
string sql = "update User set Key=@Key,Value=@Value where Id=@Id";
return conn.Execute(sql, model);
}
}
public int Del(UserModel model)
{
using (var conn = DapperConn.GetConnection())
{
string sql = "Delete from User where Id=@Id";
return conn.Execute(sql, model);
}
} public UserModel GetModel()
{
using (var conn = DapperConn.GetConnection())
{
var sql = "Select Id,Key,Value from User";
return conn.QueryFirstOrDefault<UserModel>(sql);
}
} public IEnumerable<UserModel> GetModels()
{
using (var conn = DapperConn.GetConnection())
{
var sql = "Select Id,Key,Value from User";
return conn.Query<UserModel>(sql);
}
} public void ImplementAffair(UserModel userModel, UserLoginLog userLogModel)
{
using (var conn = DapperConn.GetConnection())
{
IDbTransaction tran = conn.BeginTransaction();
try
{
string query = "Update User set Key='测试' where ID=@ID";//更新一条记录
conn.Execute(query, userModel, tran, null, null); query = "insert into UserLoginLog (userId,CreateTime) value (@userId,@CreateTime)";//删除一条记录
conn.Execute(query, userLogModel, tran, null, null); //提交
tran.Commit();
}
catch (Exception ex)
{
//提交错误
//回滚事务
tran.Rollback();
}
}
} /// <summary>
/// 执行无参数存储过程 返回列表
/// </summary>
/// <returns></returns>
private IEnumerable<UserModel> ExecuteStoredProcedureNoParms()
{
using (IDbConnection con = DapperConn.GetConnection())
{
var userList = new List<UserModel>();
userList = con.Query<UserModel>("QueryRoleNoParms",
null,
null,
true,
null,
CommandType.StoredProcedure).ToList();
return userList;
}
} /// <summary>
/// 执行无参数存储过程 返回int
/// </summary>
/// <returns></returns>
private int ExecutePROC()
{
using (IDbConnection con = DapperConn.GetConnection())
{
return con.Execute("QueryRoleWithParms", null, null, null, CommandType.StoredProcedure);
}
} /// <summary>
/// 执行带参数的存储过程
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
private string ExecutePROC(UserModel model)
{
DynamicParameters dp = new DynamicParameters();
dp.Add("@ID", "");
dp.Add("@msg", "", DbType.String, ParameterDirection.Output);
using (IDbConnection con = DapperConn.GetConnection())
{
con.Execute("Proc", dp, null, null, CommandType.StoredProcedure);
string roleName = dp.Get<string>("@msg");
return roleName;
}
}
}
}
.NET之Dapper框架运用的更多相关文章
- EF框架 与 Dapper框架 调用分页存储过程
1. SqlServer创建存储过程: --创建存储过程 create proc sp_Show ( @index int, --当前页 @size int, --每页大小 @totalcount i ...
- asp.net mvc + dapper(ORM框架) + easyui框架简洁的信息管理项目
1.目录结构: 2.效果图: 3.IndexController控制器: using System; using System.Collections; using System.Collection ...
- 我的基于asp.net mvc5 +mysql+dapper+easyui 的Web开发框架(1)数据库访问(0)
一.数据库访问 概述 1. 数据库使用mysql,orm采用dapper框架.dapper框架应用简单,只是需要自己手写sql语句,但是对于像我这样写了多年sql语句的人来说,这应该不算问题,个人还是 ...
- ORM之Dapper运用
一.前言 上一篇[分层架构设计]我们已经有了架构的轮廓,现在我们就在这个轮廓里面造轮子.项目要想开始,肯定先得确定ORM框架,目前市面上的ORM框架有很多,对于.net人员来说很容易就想到以ADO.N ...
- Dapper的封装、二次封装、官方扩展包封装,以及ADO.NET原生封装
前几天偶然看到了dapper,由于以前没有用过,只用过ef core,稍微看了一下,然后写了一些简单的可复用的封装. Dapper的用法比较接近ADO.NET所以性能也是比较快.所以我们先来看看使用A ...
- 微软.NET年芳15:我在Azure上搭建Photon服务器(C#.NET)
网上火热的“微软.NET年芳15”文章,我也得写点什么嘛,毕竟我还是现任的微软MVP. 摘录网上的“.NET 15周年”信息如下: 微软的 .NET 框架本周迎来了 15 岁生日..NET 的第一个版 ...
- 关于web程序快速开发个人见解以及经历
由于在之前公司业务的发展,需要在基于核心业务的基础上开发其他较为独立的业务系统,所以就有了这个基于Dapper,DDD概念的基础框架,由于个人基于这个框架已经经历过两个系统的开发,也因为其他项目团队需 ...
- web程序快速开发
关于web程序快速开发个人见解以及经历 由于在之前公司业务的发展,需要在基于核心业务的基础上开发其他较为独立的业务系统,所以就有了这个基于Dapper,DDD概念的基础框架,由于个人基于这个框架已经经 ...
- (1)dotnet开源电商系统-brnshop&brnMall 和老外开发的nopCommerce(dotnet两套电商来PK--第一篇)
一直想做电商软件,但是实在不想学PHP了,所以前后关注了这两个开源电商系统.一个是国人出品的,一个据说是俄罗斯人写得(不知道对不对).目前两个开源软件都在学习了解中,以下的博文可能会涉及到这两套系统, ...
随机推荐
- Get/POST方法提交的长度限制
1. Get方法长度限制 Http Get方法提交的数据大小长度并没有限制,HTTP协议规范没有对URL长度进行限制.这个限制是特定的浏览器及服务器对它的限制. 如:IE对URL长度的限制 ...
- 实例说明Java中的null
翻译人员: 铁锚 翻译时间: 2013年11月11日 原文链接: What exactly is null in Java? 让我们先来看下面的语句: String x = null; 1. 这个语句 ...
- 开源图像检索工具:Caliph&Emir使用方法
Caliph&Emir是基于MPEG7的软件.它是用Java编写的开源软件.采用了lucene完成索引和检索功能.是研究MPEG7标准,图像检索等等方面不可多得的好工具. 在此介绍一下它们的基 ...
- some phrase for oral english
依我看,在我看来 I suppose that, ... As far as i'm concerned, ... As i see it, ... It seems to me that ... 1 ...
- 在Android中afinal框架下实现sqlite数据库版本升级的办法
上一篇文章采用的是SQLiteOpenHelper中的onUpgrade方法实现数据库的升级. 首先获取Context: private Context mContext=this; 然后实现Fina ...
- Java进阶(五十二)利用LOG4J生成服务日志
Java进阶(五十二)利用LOG4J生成服务日志 前言 由于论文写作需求,需要进行流程挖掘.前提是需要有真实的事件日志数据.真实的事件日志数据可以用来发现.监控和提升业务流程. 为了获得真实的事件日志 ...
- Java-HttpServletResponse-HttpServletResponseWrapper
//继承ServletResponse,发送回复信息,servlet容器创建一个HttpServletResponse对象,将它作为service函数的参数 public interface Http ...
- 手持机设备公司(WINCE/ANDROID/LINUX)
1.深圳扬创科技有限公司网址: http://www.yctek.com/ 2.无锡盈达聚力科技有限公司 点击打开链接 3.上海鲲博通信技术有限公司(主要为用WINCE开发导航产品) 点击打开链接 4 ...
- 如何解决Asp.Net中不能上传压缩文件的问题
在使用Asp.Net自带的服务器端控件Fileupload上传文件时,可能会出现不能上传压缩文件的问题,此时可以通过下面的方法解决: 在<system.web>中添加: <httpR ...
- 【nginx】4xx,5xx 保持自定义header
问题 nginx使用中,如果请求返回的状态code类似404或者50x这种,仍然返回自定义的header. 分析和解决 nginx文档中关于 add_header的部分 有这么一句 Adds the ...