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

  1. Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示

    Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...

  2. Dapper扩展之~~~Dapper.Contrib

    平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...

  3. 由Dapper QueryMultiple 返回数据的问题得出==》Dapper QueryMultiple并不会帮我们识别多个返回值的顺序

    异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 今天帮群友整理Dapper基础教程的时候手脚快了点,然后遇到了一个小问题,Dapp ...

  4. 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 ...

  5. Dapper where Id in的解决方案

    简单记一下,一会出去有点事情~ 我们一般写sql都是==>update NoteInfo set NDataStatus=@NDataStatus where NId in (@NIds) Da ...

  6. ASP.NET Core 1.0 使用 Dapper 操作 MySql(包含事务)

    操作 MySql 数据库使用MySql.Data程序包(MySql 开发,其他第三方可能会有些问题). project.json 代码: { "version": "1. ...

  7. Asp.Net Core + Dapper + Repository 模式 + TDD 学习笔记

    0x00 前言 之前一直使用的是 EF ,做了一个简单的小项目后发现 EF 的表现并不是很好,就比如联表查询,因为现在的 EF Core 也没有啥好用的分析工具,所以也不知道该怎么写 Linq 生成出 ...

  8. 搭建一套自己实用的.net架构(3)续 【ORM Dapper+DapperExtensions+Lambda】

    前言 继之前发的帖子[ORM-Dapper+DapperExtensions],对Dapper的扩展代码也进行了改进,同时加入Dapper 对Lambda表达式的支持. 由于之前缺乏对Lambda的知 ...

  9. mono for android中使用dapper或petapoco对sqlite进行数据操作

    在mono for android中使用dapper或petapoco,很简单,新建android 类库项目,直接把原来的文件复制过来,对Connection连接报错部分进行注释和修改就可以运行了.( ...

  10. 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. ...

随机推荐

  1. 办公用品管理系统VB——库存数量导出EXCEL,SaveEXCEL

    办公用品管理系统VB——库存数量导出EXCEL,SaveEXCEL 总体来说,VB的EXCEL导出效率还是蛮低的,就是一个小型化的办公用品管理软件,不再优化了. 时间紧迫,就没有从头到尾的用C#编写, ...

  2. pyqt pyside QLabel 显示图片

    pyqt pyside QLabel 显示图片 pixmap = QtGui.QPixmap("D:/myPicture.jpg") label.setPixmap(pixmap) ...

  3. 水晶报表,快速报表,rdlc报表

    感觉自己脑子里只剩下报表了,o(╥﹏╥)o.因为最近新换了公司,业务上有需要报表打印,水晶报表,快速报表,rdlc报表这三种以后可能都会用到.所以在没了解好业务流程,熟悉代码之前,就是看看这三种报表怎 ...

  4. angular.injector()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 重构file_get_contents实现一个带超时链接访问的函数

    function wp_file_get_contents($url, $timeout = 30) { $context = stream_context_create(array( 'http' ...

  6. sql的基础用法

    # sql 对大小写不敏感 # 查询表中的所有信息 select * from `Customers`; # 查询指定字段 CustomerName,Country select CustomerNa ...

  7. HDFS的WEB页面访问常见问题

    HDFS的WEB UI管理页面 50070 端口 无法访问解决办法! 本文基于HADOOP-3..1.0,Cecntos7.0环境下进行测试,所以遇到很多新鲜的问题: 特别注意:HaDoop3.0之前 ...

  8. Spring Boot 引入org.springframework.boot.SpringApplication出错

    今天新建的一个spring boot maven项目, 写启动类时发现无法引入SpringApplication, 经查原来是冲突了,我早些时候用了比较低版本的spring boot创建了项目 ,导致 ...

  9. if-else案例–开关灯

    首先,创建一个html页面,添加一个div盒子,用css设置相应的样式,用js获取盒子的元素,通过点击事件,设置body的背景颜色,用if..else来判断当什么状态设置相应的颜色,(swith... ...

  10. archlinux下安装acroread打开pdf

    虽说acroread是个好东西,但是在打开pdf几秒后总是自动退出呢 在其aur网页下找到了这么一解决办法: 打开终端输入 sudo unshare -n sudo -u ${USER} ACRO_A ...