.net ORM框架(Dapper简单应用)

Posted zzqqrr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.net ORM框架(Dapper简单应用)相关的知识,希望对你有一定的参考价值。

1.引入 Dapper.dll类库

2.创建书籍模型book

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Book
{
    public int ID { get; set; }
 
    public string Name { get; set; }

    public string Txt { get; set; }
}

3.创建数据库帮助类(需要引用using Dapper;)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;
using System.Data.SqlClient;

public static class DBHelper
{
    private static readonly string connString = "Data Source=.;Initial Catalog=qrab;Integrated Security=False;User ID=sa;Password=111111;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;";
        //ConfigurationManager.ConnectionStrings["PharmacySystem"].ConnectionString;
    private static IDbConnection _conn;
    public static IDbConnection Conn
    {
        get
        {
            _conn = new SqlConnection(connString);
            _conn.Open();
            return _conn;
        }
    }
    /// <summary>
    /// 插入书籍
    /// </summary>
    /// <param name="book"></param>
    /// <returns></returns>
    public static int Insert(Book book)
    {
        using (Conn)
        {
            string query = "insert into Book(name,txt)values(@name,@txt)";
            return Conn.Execute(query, book);
        }
    }
    /// <summary>
    /// 更新书籍
    /// </summary>
    /// <param name="book"></param>
    /// <returns></returns>
    public static int Update(Book book)
    {
        using (Conn)
        {
            string query = "update Book set name=@name,txt=@txt where id=@id";
            return Conn.Execute(query, book);
        }
    }
    /// <summary>
    /// 删除书籍
    /// </summary>
    /// <param name="book"></param>
    /// <returns></returns>
    public static int Delete(Book book)
    {
        using (Conn)
        {
            string query = "delete from Book where id=@id";
            return Conn.Execute(query, book);
        }
    }
    /// <summary>
    /// 删除书籍
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public static int Delete(string id)
    {
        using (Conn)
        {
            string query = "delete from Book where id=@id";
            return Conn.Execute(query, new { id = id });
        }
    }
    /// <summary>
    /// 读取书籍列表
    /// </summary>
    /// <returns></returns>
    public static IList<Book> GetList()
    {
        using (Conn)
        {
            string query = "select * from Book";
            return Conn.Query<Book>(query).ToList();
        }
    }
    /// <summary>
    /// 根据ID取一本书籍
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public static Book GetEntity(string id)
    {
        Book book;
        string query = "select * from Book where id=@id";
        using (Conn)
        {
           book = Conn.Query<Book>(query, new { id = id }).SingleOrDefault();
            return book;
        }
    }

}

4.简单人增加删除修改操作

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// 显示所有书籍
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAll_Click(object sender, EventArgs e)
        {
            IList<Book> listBook = DBHelper.GetList();
            gvBookList.AutoGenerateColumns = false;
            gvBookList.DataSource = listBook;
        }
        /// <summary>
        /// 添加书籍
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            Book book1 = new Book
            {
                Name = @"1金2瓶3梅",
                Txt = @"文学著作"
            };
            DBHelper.Insert(book1);
        }
        /// <summary>
        /// 编辑书籍
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            int id = 1;//获取需要编辑书籍的ID
            Book book1 = new Book
            {
                ID=id,
                Name = @"修改Name",
                Txt = @"修改txt"
            };
            DBHelper.Update(book1);
        }
        /// <summary>
        /// 删除指定的书籍
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_Click(object sender, EventArgs e)
        {
            int id = 1;//获取需要删除书籍的ID
            DBHelper.Delete(id.ToString());
        }
    }
}

5.效果图

 上传不了附件,需要源码的可以留言。

以上是关于.net ORM框架(Dapper简单应用)的主要内容,如果未能解决你的问题,请参考以下文章

ORM 轻量级框架 Dapper(介绍)

Dapper扩展Dapper.Common框架 Linq To Sql 底层源码.net ORM框架

.NET轻量级ORM框架Dapper入门精通

.NET轻量级ORM框架Dapper入门精通

自制简单的.Net ORM框架 简介

ORM框架之------Dapper,Net下无敌的ORM