码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-11-03 00:10:18      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:blog   oid   thread   rom   com   删除   out   stat   win   

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简单应用)

标签:blog   oid   thread   rom   com   删除   out   stat   win   

原文地址:http://www.cnblogs.com/zqrios/p/7775221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!