• Dapper 基础用法


    Dapper是.Net下的一个简单orm框架,具有以下特点:

    1、简单,只需要一个文件即可(SqlMapper.cs)

    2、快速,下面是一个查询结果集在500以上的运行速度对比

    3、不要求特定的db provider,即不论是sqlite, sqlce, firebird, oracle, MySQL 还是 SQL Server,都能使用它。

    项目的github地址是:https://github.com/SamSaffron/dapper-dot-net 可以从这里下载SqlMapper.cs,然后添加到自己的项目中即可。

    下面通过一个例子来介绍Dapper的基本用法。

    假设有个数据库test,test包含一张表users用来存储id, nickname, email, role字段。

    在vs中新建一个控制台项目,添加SqlMapper.cs到项目中,下面是program.cs的内容,注释中介绍了dapper的常用方法:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Data;
      4 using System.Data.SqlClient;
      5 using System.Linq;
      6 using System.Text;
      7 using Dapper;
      8 
      9 namespace DapperTest
     10 {
     11     class Program
     12     {
     13         public const string ConnectionString = "Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=test";
     14 
     15         public const string OleDbConnectionString = "Provider=SQLOLEDB;Data Source=.;Initial Catalog=tempdb;Integrated Security=SSPI";
     16 
     17         public static SqlConnection GetOpenConnection()
     18         {
     19             var connection = new SqlConnection(ConnectionString);
     20             connection.Open();
     21             return connection;
     22         }
     23 
     24         static void Main(string[] args)
     25         {
     26             var connection = GetOpenConnection();
     27 
     28             //1、返回动态对象列表的查询
     29             Console.WriteLine("1、返回动态对象列表的查询");
     30             var users = connection.Query("select * from users");
     31             foreach (var user in users)
     32             {
     33                 //动态对象的名称与数据库字段的名称一模一样
     34                 Console.WriteLine(user.id+"	"+user.nickname+"	"+user.email);
     35             }
     36             Console.WriteLine(users.First().nickname);
     37 
     38             //2、返回强类型列表的查询
     39             Console.WriteLine("2、返回强类型列表的查询");
     40             var users2 = connection.Query<User>("select * from users");
     41             foreach (var user in users2)
     42             {
     43                 //注意强类型返回的是User类实例
     44                 Console.WriteLine(user.Id + "	" + user.NickName + "	" + user.Email);
     45             }
     46 
     47             //3、传递参数的查询
     48             Console.WriteLine("3、传递参数的查询");
     49             var users3 = connection.Query<User>("select * from users where email=@email",
     50                 new {email = "liulixiang1988@gmail.com"});
     51             foreach (var user in users3)
     52             {
     53                 //注意强类型返回的是User类实例
     54                 Console.WriteLine(user.Id + "	" + user.NickName + "	" + user.Email);
     55             }
     56 
     57             //4、执行不返回结果的命令(更新、删除等)
     58             Console.WriteLine("4、执行不返回结果的命令(更新、删除等)");
     59             connection.Execute(@"
     60               IF EXISTS(SELECT 1 FROM users WHERE nickname=@nickname)
     61                 RETURN;
     62               insert into users(nickname, email, role) values(@nickname, @email, @role);
     63             ", new {nickname = "王二", email = @"wanger@test.com", role = 0});
     64             var users4 = connection.Query<User>("select * from users");
     65             Console.WriteLine("执行不返回结果后的结果集合");
     66             foreach (var user in users4)
     67             {
     68                 //注意强类型返回的是User类实例
     69                 Console.WriteLine(user.Id + "	" + user.NickName + "	" + user.Email);
     70             }
     71 
     72             //5、插入或更新集合或者列表(多条记录)
     73             Console.WriteLine("5、插入或更新集合或者列表(多条记录)");
     74             var userList = new List<User>
     75             {
     76                 new User() {NickName = "李四", Email = "lisi@test.com", Role = 0},
     77                 new User() {NickName = "张三", Email = "zhangsan@test.com", Role = 0}
     78             };
     79             connection.Execute(@"
     80               IF EXISTS(SELECT 1 FROM users WHERE nickname=@nickname)
     81                 RETURN;
     82               insert into users(nickname, email, role) values(@nickname, @email, @role);
     83             ", userList);
     84             //执行查询
     85             var users5 = connection.Query<User>("select * from users");
     86             Console.WriteLine("执行不返回结果后的结果集合");
     87             foreach (var user in users5)
     88             {
     89                 //注意强类型返回的是User类实例
     90                 Console.WriteLine(user.Id + "	" + user.NickName + "	" + user.Email);
     91             }
     92             //6、dapper支持列表自动分解
     93             Console.WriteLine("6、dapper支持列表自动分解");
     94             var users6 = connection.Query<User>(
     95                 @"SELECT * FROM users
     96                 WHERE nickname IN @nicknames", new {nicknames=new string[]{"张三", "李四"}})
     97             ;
     98             foreach (var user in users6)
     99             {
    100                 //注意强类型返回的是User类实例
    101                 Console.WriteLine(user.Id + "	" + user.NickName + "	" + user.Email);
    102             }
    103 
    104             //7、执行存储过程
    105             Console.WriteLine("7、执行存储过程");
    106             var users7 = connection.Query<User>("sp_get_user", new {nickname = "张三"},
    107                 commandType: CommandType.StoredProcedure);
    108             foreach (var user in users7)
    109             {
    110                 //注意强类型返回的是User类实例
    111                 Console.WriteLine(user.Id + "	" + user.NickName + "	" + user.Email);
    112             }
    113             Console.ReadKey();
    114             if(connection.State != ConnectionState.Closed)
    115                 connection.Close();
    116         }
    117     }
    118 
    119     public class User
    120     {
    121         public int? Id { get; set; }
    122         public string NickName { get; set; }
    123         public string Email { get; set; }
    124         public int? Role { get; set; }
    125     }
    126 }
  • 相关阅读:
    html自定义加载动画
    数据结构与算法:贪心算法简介
    WEB 前端常用字体和色值你知道多少?
    为什么浮点精度运算会有问题
    Java 字符串格式示例
    使用 Java 和 Maven (JBake) 生成静态网站
    硅谷钢铁侠
    Flink中的5种窗口使用场景
    prometheus-operator监控k8s资源
    fluentd收集k8s集群pod日志
  • 原文地址:https://www.cnblogs.com/liulixiang/p/dapper-dot-net.html
Copyright © 2020-2023  润新知