dajpper使用教程

1.表结构

Image text
Image text

2、程序对应的实体类

Image text
Image text

3、基本操作

  • 3.1 插入

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public int Insert(Person person, string _ConnString)
    {
    using (IDbConnection connection = new SqlConnection(_ConnString))
    {
    return connection.Execute("insert into Person(Name,Remark) values(@Name,@Remark)", person);

    }

    }
  • 3.2 删除

    1
    2
    3
    4
    5
    6
    7
    public int Delete(Person person, string connectionString)
    {
    using (IDbConnection connection = new SqlConnection(connectionString))
    {
    return connection.Execute("delete from Person where id=@ID", person);
    }
    }
  • 3.3 修改

    1
    2
    3
    4
    5
    6
    7
    public int Update(Person person, string connectionString)
    {
    using (IDbConnection connection = new SqlConnection(connectionString))
    {
    return connection.Execute("update Person set name=@Name where id=@ID", person);
    }
    }
  • 3.4 查询

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    /// <summary>
    /// 批量修改
    /// </summary>
    /// <param name="persons"></param>
    /// <param name="connectionString"></param>
    /// <returns></returns>
    public int Update(List<Person> persons, string connectionString)
    {
    using (IDbConnection connection = new SqlConnection(connectionString))
    {
    return connection.Execute("update Person set name=@name where id=@ID", persons);
    }
    }


    /// <summary>
    /// 无参查询所有数据
    /// </summary>
    /// <returns></returns>
    public List<Person> Query(string connectionString)
    {
    using (IDbConnection connection = new SqlConnection(connectionString))
    {
    return connection.Query<Person>("select * from Person").ToList();
    }
    }
  • 其余内容:https://hongmaju.github.io/2018/06/19/Dapper%E7%9A%84%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E/#more
原文地址:https://www.cnblogs.com/hongmaju/p/9210937.html