Dapper的应用
Dapper是一个轻型的开源ORM类,代码就一个SqlMapper.cs文件
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web; namespace WcfServiceTest
{
/// <summary>
/// Dapper案例
/// </summary>
public class StudentDB
{
//获取web.config里的连接字符串
public static string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectstring"]; /// <summary>
/// 查询指定数据
/// </summary>
/// <param name="person"></param>
/// <returns></returns>
public static List<Student> Query(string name)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
string strsql = "select * from Student where 1=1 ";
if (!string.IsNullOrEmpty(name))
{
strsql += "and Name like '%@Name%'";
}
return connection.Query<Student>(strsql, name).ToList();
}
} /// <summary>
/// 反填
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static Student QueryById(int id)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Query<Student>("select * from Student where Id=@Id", id).SingleOrDefault();
}
} /// <summary>
/// 新增
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public static int Insert(Student student)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Execute("insert into Student(Name,Age) values(@Name,@Age)", student);
}
} public static int Update(Student student)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Execute("update Studentset Name = @Name,Age= @Age where Id=@Id", student);
}
} /// <summary>
/// 删除
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public static int Delete(int Id)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Execute("delete from Student where Id=@Id", Id);
}
} /// <summary>
/// 批量删除
/// </summary>
/// <param name="students"></param>
/// <returns></returns>
public static int Delete(List<Student> students)
{
using (IDbConnection connection = new SqlConnection(connectionString))
{
return connection.Execute("delete from Student where Id=@Id", students);
}
} }
}
Dapper的应用的更多相关文章
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- Dapper扩展之~~~Dapper.Contrib
平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...
- 由Dapper QueryMultiple 返回数据的问题得出==》Dapper QueryMultiple并不会帮我们识别多个返回值的顺序
异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 今天帮群友整理Dapper基础教程的时候手脚快了点,然后遇到了一个小问题,Dapp ...
- Dapper.Contrib:GetAsync<T> only supports an entity with a [Key] or an [ExplicitKey] property
异常处理:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 原来Model是这样滴 修改后是这样滴 注意点:Model里面的Table和Key ...
- Dapper where Id in的解决方案
简单记一下,一会出去有点事情~ 我们一般写sql都是==>update NoteInfo set NDataStatus=@NDataStatus where NId in (@NIds) Da ...
- ASP.NET Core 1.0 使用 Dapper 操作 MySql(包含事务)
操作 MySql 数据库使用MySql.Data程序包(MySql 开发,其他第三方可能会有些问题). project.json 代码: { "version": "1. ...
- Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记
0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...
- 搭建一套自己实用的.net架构(3)续 【ORM Dapper+DapperExtensions+Lambda】
前言 继之前发的帖子[ORM-Dapper+DapperExtensions],对Dapper的扩展代码也进行了改进,同时加入Dapper 对Lambda表达式的支持. 由于之前缺乏对Lambda的知 ...
- mono for android中使用dapper或petapoco对sqlite进行数据操作
在mono for android中使用dapper或petapoco,很简单,新建android 类库项目,直接把原来的文件复制过来,对Connection连接报错部分进行注释和修改就可以运行了.( ...
- Dapper:The member of type SeoTKD cannot be used as a parameter Value
异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 上次说了一下Dapper的扩展Dapper.Contrib http://www. ...
随机推荐
- Spring Boot整合邮件发送
概述 Spring Boot下面整合了邮件服务器,使用Spring Boot能够轻松实现邮件发送:整理下最近使用Spring Boot发送邮件和注意事项: Maven包依赖 <dependenc ...
- Dapper结合Repository模式的应用
Dapper结合Repository模式的应用,包括如何在数据访问层(DAL)使用Dapper组件. Dapper在真实项目中使用,扩展IDbConnection的功能,支持Oracle.MS SQL ...
- Tomcat Getshell
安装环境 账号密码路径:Tomcat6.0/conf/tomcat-users.xml 弱口令扫描工具 后台默认登陆地址:html://xx.xx.xx.xx/manager/html 后台war f ...
- c#堆与栈
一.在讲堆栈之前,我们先看看值类型和引用类型: 1,我们看看值类型与引用类型的存储方式: 引用类型:引用类型存储在堆中.类型实例化的时候,会在堆中开辟一部分空间存储类的实例.类对象的引用还是存储在栈中 ...
- Ubuntu14.0使用gparted调整分区大小
不知道为什么,我总会碰到一些疑难杂症,别人的分区都是在同一个目录下,直接通过,不断调整同一目录下相邻分区之间的空间来达到调整目标分区大小的目的 但我的不一样,我的主要分区在扩展分区下,极其魔性,图片里 ...
- 2018.我的NOIP补全计划
code: efzoi.tk @ shleodai noip2011 D1 选择客栈 这道题是一道大水题,冷静分析一会就会发现我们需要维护最后一个不合法点和前缀和. 维护最后一个不合法点只要边扫描边维 ...
- [jzoj]2938.【NOIP2012模拟8.9】分割田地
Link https://jzoj.net/senior/#main/show/2938 Description 地主某君有一块由2×n个栅格组成的土地,有k个儿子,现在地主快要终老了,要把这些土地分 ...
- js中return false; jquery中需要这样写:return false(); Jquery 中循环 each的用法 for循环
js中return false; jquery中需要这样写:return false(); Jquery 中循环 each的用法 $(".progressName").each(f ...
- 100837D
囤了一个星期..今天看了下vj上 sysuteam7 三年半之前的代码.. 深刻地认识到了自己智商不足的问题. 先求出来每个点对中心的偏移量.确实是乱序的,但是我们可以极角排序,这样一定是一个循环移位 ...
- css 控制文字显示两行,多余用省略号 手机端
p { width:100px; position:relative; line-height:20px; /*行高为高度的一半,这样就是两行*/ height:40px; overflow:hidd ...