1. 程式人生 > >.net ORM框架(Dapper簡單應用)

.net ORM框架(Dapper簡單應用)

tostring 書籍 config tlist get string event tegra connect

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簡單應用)