.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了,所以前后关注了这两个开源电商系统.一个是国人出品的,一个据说是俄罗斯人写得(不知道对不对).目前两个开源软件都在学习了解中,以下的博文可能会涉及到这两套系统, ...
随机推荐
- 16_Android生命周期再介绍,通过androidconfigChanges属性让界面旋转时不改变状态中保留的值
A android:configChanges属性 对android:configChanges属性,一般认为有以下几点: 1 不设置Activity的android:configChange ...
- 【一天一道LeetCode】#58. Length of Last Word
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
- AngularJS中的依赖注入
依赖注入 | Dependency Injection 原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所 ...
- Cocos2D中相关问题提问的几个论坛
如果和SpriteBuilder相关可以到: http://forum.spritebuilder.com 提问. 如果是Cocos2D的问题,则可以到以下论坛询问: http://forum.coc ...
- how tomcat works 读书笔记(二)----------一个简单的servlet容器
app1 (建议读者在看本章之前,先看how tomcat works 读书笔记(一)----------一个简单的web服务器 http://blog.csdn.net/dlf123321/arti ...
- 框架页面高度自动刷新Javascript脚本
实现原理:加载index.htm时候,每隔1秒钟自动调用脚本刷新框架页面代码 代码优点:只需要设置index.html框架页面中的脚本,调用加载的子页面中不需要设置任何代码. index.htm代码如 ...
- redis注册成window服务
注册服务 redis-server.exe –service-install redis.windows.conf 删除服务 redis-server –service-uninstall 开启服务 ...
- 初探linux子系统集之i2c子系统(一)
I2c子系统在进公司来的时候就学习过了,可是那是还不是很熟悉linux中的i2c子系统,就没有细看.记得当初很想熟悉linux中的各种总线驱动,想专门写一个关于总线驱动的专集,后来发现好像就没有几个, ...
- 【11】-java递归和非递归二叉树前序中序后序遍历
二叉树的遍历 对于二叉树来讲最主要.最基本的运算是遍历. 遍历二叉树 是指以一定的次序访问二叉树中的每个结点.所谓 访问结点 是指对结点进行各种操作的简称.例如,查询结点数据域的内容,或输出它的值,或 ...
- asp.net 调试与iis部署的问题
第一个问题:编译器错误信息: CS0016: 未能写入输出文件"c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET ...