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框架运用的更多相关文章

  1. EF框架 与 Dapper框架 调用分页存储过程

    1. SqlServer创建存储过程: --创建存储过程 create proc sp_Show ( @index int, --当前页 @size int, --每页大小 @totalcount i ...

  2. asp.net mvc + dapper(ORM框架) + easyui框架简洁的信息管理项目

    1.目录结构: 2.效果图: 3.IndexController控制器: using System; using System.Collections; using System.Collection ...

  3. 我的基于asp.net mvc5 +mysql+dapper+easyui 的Web开发框架(1)数据库访问(0)

    一.数据库访问 概述 1. 数据库使用mysql,orm采用dapper框架.dapper框架应用简单,只是需要自己手写sql语句,但是对于像我这样写了多年sql语句的人来说,这应该不算问题,个人还是 ...

  4. ORM之Dapper运用

    一.前言 上一篇[分层架构设计]我们已经有了架构的轮廓,现在我们就在这个轮廓里面造轮子.项目要想开始,肯定先得确定ORM框架,目前市面上的ORM框架有很多,对于.net人员来说很容易就想到以ADO.N ...

  5. Dapper的封装、二次封装、官方扩展包封装,以及ADO.NET原生封装

    前几天偶然看到了dapper,由于以前没有用过,只用过ef core,稍微看了一下,然后写了一些简单的可复用的封装. Dapper的用法比较接近ADO.NET所以性能也是比较快.所以我们先来看看使用A ...

  6. 微软.NET年芳15:我在Azure上搭建Photon服务器(C#.NET)

    网上火热的“微软.NET年芳15”文章,我也得写点什么嘛,毕竟我还是现任的微软MVP. 摘录网上的“.NET 15周年”信息如下: 微软的 .NET 框架本周迎来了 15 岁生日..NET 的第一个版 ...

  7. 关于web程序快速开发个人见解以及经历

    由于在之前公司业务的发展,需要在基于核心业务的基础上开发其他较为独立的业务系统,所以就有了这个基于Dapper,DDD概念的基础框架,由于个人基于这个框架已经经历过两个系统的开发,也因为其他项目团队需 ...

  8. web程序快速开发

    关于web程序快速开发个人见解以及经历 由于在之前公司业务的发展,需要在基于核心业务的基础上开发其他较为独立的业务系统,所以就有了这个基于Dapper,DDD概念的基础框架,由于个人基于这个框架已经经 ...

  9. (1)dotnet开源电商系统-brnshop&brnMall 和老外开发的nopCommerce(dotnet两套电商来PK--第一篇)

    一直想做电商软件,但是实在不想学PHP了,所以前后关注了这两个开源电商系统.一个是国人出品的,一个据说是俄罗斯人写得(不知道对不对).目前两个开源软件都在学习了解中,以下的博文可能会涉及到这两套系统, ...

随机推荐

  1. Ubuntu15.04 + Matlab2014a + MatConvNet install and compile

    MatConvNet is a MATLAB toolbox implementingConvolutional NeuralNetworks (CNNs) for computer vision a ...

  2. ExtJS:菜单ComboBox及级联菜单应用

    首页一级菜单查询分组,二级菜单查询分组中的车辆信息. 定义分组数据模型: Ext.define( 'group', { extend:'Ext.data.Model', fields:[ {name: ...

  3. 【LaTeX排版】LaTeX论文排版<四>

    1.表格的插入     一般的表格插入的代码如下: \begin{table}[H] \centering \begin{tabular}{|c|c|c|} \hline 感知方法&优点&am ...

  4. 苹果新的编程语言 Swift 语言进阶(一)--综述

    Swift 是苹果开发和提供的供开发IOS 和OS X应用的一门新的语言.Swift语言基于C 和Objective-C语言,除了提供C 和Objective-C语言具有的所有语法功能外,为了编程方便 ...

  5. myeclipse不编译

    错误: org.eclipse.core.internal.registry.configurationElementHandle cannot be cast to org.eclipse.jdt. ...

  6. window7如何配置修改环境变量

    http://jingyan.baidu.com/article/b24f6c82cba6dc86bfe5da9f.html

  7. LeetCode之旅(15)-Odd Even Linked List

    题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

  8. C语言中如何写一个简单可移植而又足够随机的随机数生成器

    在C语言中标准库中的随机数产生函数的返回可能不是最优的,因为有些随机数生成器的低位并不随机,而另一些返回随机数的函数实现上又太复杂鸟.所以rand()%N并不是一个好方法,牛人给出的建议是使用: ra ...

  9. 二维码js生成库

    jr-qrcode 把字符串生成二维码,并以Base64 URL形式输出. 支持白色二维码,即反色二维码. 兼容性 插件使用了H5的canvas特性进行二维码绘制,最后输出base64 url,因此本 ...

  10. OpenStack初识

    一.它可以用来做什么? 想认识一个事物,必须先弄明白它是什么,能干什么.首先说一下,openstack是一个搭建云平台的一个解决方案,说他不是个软件,但是我觉得说是一个软件,能够让初学者更容易接受和理 ...