关于 Dapper 的介绍,请参考:Dapper - 一款轻量级对象关系映射(ORM)组件,DotNet 下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Dapper; namespace SqlMapperDemo.ConApp
{
public interface IUser
{
string Key { get; set; }
string Name { get; set; }
} public class UserInfo : IUser
{
public string Key { get; set; }
public string Name { get; set; }
} public class DbContext
{
private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString; public static IDbConnection GetConnection()
{
var connection = new SqlConnection(ConnectionString);
connection.Open();
return connection;
} public static int GetAccountCount(string key)
{
using (var db = GetConnection())
{
return db.Query<int>("select count(*) from Account where UserKey = @key", new { key }).FirstOrDefault();
}
} public static void Add(IUser user)
{
using (var db = GetConnection())
{
db.Execute("if not exists(select 1 from Account where UserKey = @key) " +
"insert into Account(UserKey, UserName) values (@key, @name);" +
"else " +
"update Account set UserName = @name, deleted = null where UserKey = @key;",
new
{
name = user.Name,
key = user.Key
});
}
} public static void Remove(string key)
{
using (var db = GetConnection())
{
db.Execute("delete Account where UserKey = @key",
new
{
key
});
}
} public static IList<IUser> GetList()
{
using (var db = GetConnection())
{
return db.Query<UserInfo>("select UserKey as [Key], UserName as Name from Account " +
"where 1=1").ToList().Cast<IUser>().ToList();
}
}
} class Program
{
static void Main(string[] args)
{
string userKey = Guid.NewGuid().ToString(); // demo-01 - 得到
Console.WriteLine("accountCount:" + DbContext.GetAccountCount(userKey)); // 0 // demo-02 - 新增
DbContext.Add(new UserInfo() { Key = userKey, Name = "小张" }); // demo-03 - 再次得到
Console.WriteLine("accountCount:" + DbContext.GetAccountCount(userKey)); // 1 // demo-04 - 得到所有记录
foreach (IUser userItem in DbContext.GetList())
{
Console.WriteLine("userKey: " + userItem.Key + ", userName: " + userItem.Name);
//userKey: f0cbf560-9c41-41a7-9ccf-31026adbfa91, userName: 小张
} // demo-05 - 删除
DbContext.Remove(userKey); // demo-06 - 删除后,得到
Console.WriteLine("accountCount:" + DbContext.GetAccountCount(userKey)); //
} }
}

谢谢浏览!

一小段 Dapper 的简单示例的更多相关文章

  1. 微型orm框架--dapper的简单使用

    1.安装 首先使用nuget安装dapper,因为这里的示例是使用mysql,所以还要安装mysql的驱动.如下图: 2 数据库表 脚本 ; -- -------------------------- ...

  2. pureMVC简单示例及其原理讲解四(Controller层)

    本节将讲述pureMVC示例中的Controller层. Controller层有以下文件组成: AddUserCommand.as DeleteUserCommand.as ModelPrepCom ...

  3. SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论 SignalR 简单示例 通过三个DEMO学会SignalR的三种实现方式 SignalR推送框架两个项目永久连接通讯使用 SignalR 集线器简单实例2 用SignalR创建实时永久长连接异步网络应用程序

    SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论   异常汇总:http://www ...

  4. java读取ACCESS数据库的简单示例

    java读取ACCESS数据库的简单示例 虽然简单,对初学者来说,如果没有一段可以成功执行的代码供参考,还真难调试 先用ACCESS建一个数据库 DB1.MDB,里面有一表"table1&q ...

  5. C#调用Python脚本的简单示例

    C#调用Python脚本的简单示例 分类:Python (2311)  (0)  举报  收藏 IronPython是一种在 .NET及 Mono上的 Python实现,由微软的 Jim Huguni ...

  6. Cookie是存储在客户端上的一小段数据

    背景 在HTTP协议的定义中,采用了一种机制来记录客户端和服务器端交互的信息,这种机制被称为cookie,cookie规范定义了服务器和客户端交互信息的格式.生存期.使用范围.安全性. 在JavaSc ...

  7. 并行编程OpenMP基础及简单示例

    OpenMP基本概念 OpenMP是一种用于共享内存并行系统的多线程程序设计方案,支持的编程语言包括C.C++和Fortran.OpenMP提供了对并行算法的高层抽象描述,特别适合在多核CPU机器上的 ...

  8. WebGL简易教程(一):第一个简单示例

    目录 1. 概述 2. 示例:绘制一个点 1) HelloPoint1.html 2) HelloPoint1.js (1) 准备工作 (2) 着色器 (3) 顶点着色器 (4) 片元着色器 (5) ...

  9. thrift简单示例 (go语言)

    这个thrift的简单示例来自于官网 (http://thrift.apache.org/tutorial/go), 因为官方提供的例子简单易懂, 所以没有必要额外考虑新的例子. 关于安装的教程, 可 ...

随机推荐

  1. swoole的process模块创建和使用子进程

    swoole中为我们提供了一个进程管理模块 Process,替换PHP的 pcntl 扩展,方便我们创建进程,管理进程,和进程间的通信. swoole提供了2种进程间的通信: 1.基于 unix so ...

  2. C++ 类的前向声明的用法

    我们知道C++的类应当是先定义,然后使用.但在处理相对复杂的问题.考虑类的组合时,很可能遇到俩个类相互引用的情况,这种情况称为循环依赖. 例如: class A { public: void f(B ...

  3. Scala,Java,Python 3种语言编写Spark WordCount示例

    首先,我先定义一个文件,hello.txt,里面的内容如下: hello sparkhello hadoophello flinkhello storm Scala方式 scala版本是2.11.8. ...

  4. 【centOS】centOS7 下载

    地址:http://mirrors.aliyun.com/centos/ 进入国内的阿里云的,这里CentOS 7提供了三种ISO镜像文件的下载:DVD ISO.Everything ISO.Mini ...

  5. wpf file embeded resource is readonly,Copy always will copy the file and its folder to the bin folder

    Wpf file embeded resource will compile the file into the assembly and it will be readonly and can no ...

  6. PDF目录编辑器使用介绍

    PDF目录编辑器使用介绍 魏刘宏 2019.06.28 PDF 是一个比较优秀的文档格式,能够保证在任何终端显示的样式是一样的.但同时也带来了一个问题,就是编辑不方便,其实这也是这个格式特意为之的,无 ...

  7. Java生鲜电商平台-商城后台架构与原型图实战

    Java生鲜电商平台-商城后台架构与原型图实战 说明:生鲜电商平台的运营平台,其中需要很多的功能进行管理.目前把架构与原型图实战分享给大家,希望对大家有用. 仪表盘/首页,简单统计,报表页,运营快捷口 ...

  8. Springboot vue 前后分离 跨域 Activiti6 工作流 集成代码生成器 shiro权限

    官网:www.fhadmin.org 特别注意: Springboot 工作流  前后分离 + 跨域 版本 (权限控制到菜单和按钮) 后台框架:springboot2.1.2+ activiti6.0 ...

  9. SpringCloud之API网关与服务发现——Cloud核心组件实战入门及原理

    微服务发展历史 单体模式——>服务治理(服务拆分)——>微服务(细分服务)——>Segments(服务网格) 微服务 VS SOA 微服务:模块化.独立部署.异构化 SOA:共同的治 ...

  10. 为Dynamics CRM的Office附件注释定制个无需下载即可在线查看的功能

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复164或者20151021可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 上一篇博客:为Dynamics ...