1. Introduction of the Dapper.dll class library
2. Create Book Model Books
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks; Public classbook{ Public intID {Get;Set; } Public stringName {Get;Set; } Public stringTXT {Get;Set; }}
3. Create a Database helper class (you need to reference using Dapper;)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingDapper;usingSystem.Data;usingSystem.Data.SqlClient; Public Static classdbhelper{Private Static ReadOnly stringConnString ="Data Source=.;i Nitial 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 StaticIDbConnection _conn; Public Staticidbconnection Conn {Get{_conn=NewSqlConnection (connstring); _conn. Open (); return_conn; } } /// <summary> ///Insert a book/// </summary> /// <param name= "book" ></param> /// <returns></returns> Public Static intInsert (book book) {using(Conn) {stringquery ="INSERT into book (name,txt) VALUES (@name, @txt)"; returnconn.execute (query, book); } } /// <summary> ///Update a book/// </summary> /// <param name= "book" ></param> /// <returns></returns> Public Static intUpdate (book) {using(Conn) {stringquery ="Update book set [email protected],[email protected] where [email protected]"; returnconn.execute (query, book); } } /// <summary> ///Delete a book/// </summary> /// <param name= "book" ></param> /// <returns></returns> Public Static intDelete (book book) {using(Conn) {stringquery ="delete from book where [email protected]"; returnconn.execute (query, book); } } /// <summary> ///Delete a book/// </summary> /// <param name= "id" ></param> /// <returns></returns> Public Static intDelete (stringID) {using(Conn) {stringquery ="delete from book where [email protected]"; returnConn.execute (Query,New{id =ID}); } } /// <summary> ///reading list of books/// </summary> /// <returns></returns> Public StaticIlist<book>GetList () {using(Conn) {stringquery ="SELECT * from book"; returnConn.query<book>(query). ToList (); } } /// <summary> ///take a book by ID/// </summary> /// <param name= "id" ></param> /// <returns></returns> Public StaticBook GetEntity (stringID) {book book; stringquery ="SELECT * from book where [email protected]"; using(Conn) { Book= Conn.query<book> (Query,New{id =ID}). Singleordefault (); returnBook ; } }}
4. Simple people add Delete modify action
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacewindowsformsapp2{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) { } /// <summary> ///Show All Books/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidBtnall_click (Objectsender, EventArgs e) {IList<Book> Listbook =dbhelper.getlist (); Gvbooklist.autogeneratecolumns=false; Gvbooklist.datasource=Listbook; } /// <summary> ///Add a book/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidbtnAdd_Click (Objectsender, EventArgs e) {Book Book1=NewBook {Name=@ "1Gold 2 bottle 3 plum", Txt=@"Literary Works" }; Dbhelper.insert (BOOK1); } /// <summary> ///Edit a book/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidBtnedit_click (Objectsender, EventArgs e) { intID =1;//get the ID of the book you need to editBook Book1 =NewBook {ID=ID, Name=@"Modify Name", Txt=@"Modify TXT" }; Dbhelper.update (BOOK1); } /// <summary> ///Delete a specified book/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidBtndel_click (Objectsender, EventArgs e) { intID =1;//get the ID of the book you want to deleteDbhelper.delete (ID. ToString ()); } }}
5.
Cannot upload the attachment, need source code can leave a message.
. NET ORM Framework (dapper simple application)