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

來源:互聯網
上載者:User

標籤: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 [email protected],[email protected] where [email protected]";            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 [email protected]";            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 [email protected]";            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 [email protected]";        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簡單應用)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.