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. tensorflow变量-【老鱼学tensorflow】

    在程序中定义变量很简单,只要定义一个变量名就可以,但是tensorflow有点类似在另外一个世界,因此需要通过当前的世界中跟tensorlfow的世界中进行通讯,来告诉tensorflow的世界中定义 ...

  2. Leetcode 记录(201~300)

    实习面试前再完成100题,争取能匀速解释清楚题 204. Count Primes 素数筛 class Solution { public: int countPrimes(int n) { ) ; ...

  3. Qt写websocketpp服务端

    1.下载websocketpp,地址为https://github.com/zaphoyd/websocketpp,版本为0.7. 2.下载boost,地址为https://www.boost.org ...

  4. flask之入门

    本篇导航: Flask介绍 简单使用 排错 小结 一. Flask介绍 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug ...

  5. BOM 浏览器对象模型_不超过 4 KB 的 document.cookie 对象

    注意: 客户端储存应该使用 Web storage API 和 IndexedDB,不推荐使用 Cookie document.cookie 对象 是服务器保存在浏览器的一小段文本信息 用于读写当前网 ...

  6. 蓝桥杯-加法变乘法(java)

    蓝桥杯第六届省赛题目-加法变乘法(java) 题目: 我们都知道:1+2+3+ ... + 49 = 1225 现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015 比如: 1+2+3+... ...

  7. angularjs知识点

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Autofac之自动装配

    从容器中的可用服务中选择一个构造函数来创造对象,这个过程叫做自动装配.这个过程是通过反射实现的 默认 思考这么一个问题,如果注册类型中存在多个构造函数,那么Autofac会选择哪一个来创建类型的实例 ...

  9. char和QChar(Unicode的编码与内存里的值还不是一回事)

    char类型是c/c++中内置的类型,描述了1个字节的内存信息的解析.比如: char gemfield=’g’;那么在由gemfield标记的这块内存的大小就是1个字节,信息就是01100111,8 ...

  10. oracle根据某个字段的值进行排序

    需求:按照颜色为蓝色.红色.黄色进行排序: order by  case                  when color = '蓝色' then                   1     ...