登山则情满于山,观海则意溢于海。这篇文章主要讲述Dapper的应用相关的知识,希望能为你提供帮助。
dapper开发效率极快so easy
using Dapper; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; namespace WcfServiceTest { /// < summary> /// Dapper案例 /// < /summary> public class StudentDB { //获取web.config里的连接字符串 public static string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectstring"]; /// < summary> /// 查询指定数据 /// < /summary> /// < param name="person"> < /param> /// < returns> < /returns> public static List< Student> Query(string name) { using (IDbConnection connection = new SqlConnection(connectionString)) { string strsql = "select * from Student where 1=1 "; if (!string.IsNullOrEmpty(name)) { strsql += "and Name like ‘%@Name%‘"; } return connection.Query< Student> (strsql, name).ToList(); } }/// < summary> /// 反填 /// < /summary> /// < param name="id"> < /param> /// < returns> < /returns> public static Student QueryById(int id) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Query< Student> ("select * from Student where [email protected]", id).SingleOrDefault(); } }/// < summary> /// 新增 /// < /summary> /// < param name="student"> < /param> /// < returns> < /returns> public static int Insert(Student student) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("insert into Student(Name,Age) values(@Name,@Age)", student); } }public static int Update(Student student) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("update Studentset Name = @Name,Age= @Age where [email protected]", student); } }/// < summary> /// 删除 /// < /summary> /// < param name="student"> < /param> /// < returns> < /returns> public static int Delete(int Id) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("delete from Student where [email protected]", Id); } }/// < summary> /// 批量删除 /// < /summary> /// < param name="students"> < /param> /// < returns> < /returns> public static int Delete(List< Student> students) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("delete from Student where [email protected]", students); } }} }
【Dapper的应用】
推荐阅读
- [Android] Android 使用 Greendao 操作 db sqlite-- 封装DaoUtils类1
- 如何构建一个没有编码的移动应用程序()
- 5个最好的Bootstrap替代工具推荐
- 11种促进Android应用开发的工具
- 你应该知道的10个最佳JavaScript框架
- 9种构建小型企业应用程序的最佳Python框架
- 初学者的区块链介绍
- 如何成为前端开发人员(2020版)()
- 适用于前端开发人员的10个最佳CSS框架