Dapper数据操作层使用
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using Dapper; using System.Data.SqlClient; using System.Data; namespace YuPen.DAL { public class DapperHelper<T> where T : class { /// <summary> /// 数据库连接字符串 /// </summary> public static readonly string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; /// <summary> /// 查询列表 /// </summary> /// <param name="sql">查询的sql</param> /// <param name="param">替换参数</param> /// <returns></returns> public static List<T> Query(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.Query<T>(sql, param).ToList(); } } /// <summary> /// 查询第一个数据 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QueryFirst(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.QueryFirst<T>(sql, param); } } /// <summary> /// 查询第一个数据没有返回默认值 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QueryFirstOrDefault(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.QueryFirstOrDefault<T>(sql, param); } } /// <summary> /// 查询单条数据 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QuerySingle(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.QuerySingle<T>(sql, param); } } /// <summary> /// 查询单条数据没有返回默认值 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T QuerySingleOrDefault(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.QuerySingleOrDefault<T>(sql, param); } } /// <summary> /// 曾删改 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static int Execute(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.Execute(sql, param); } } /// <summary> /// Reader获取数据 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static IDataReader ExecuteReader(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.ExecuteReader(sql, param); } } /// <summary> /// Scalar获取数据 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static object ExecuteScalar(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.ExecuteScalar(sql, param); } } /// <summary> /// Scalar获取数据 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static T ExecuteScalarForT(string sql, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { return con.ExecuteScalar<T>(sql, param); } } /// <summary> /// 带参数的存储过程 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static List<T> ExecutePro(string proc, object param) { using (SqlConnection con = new SqlConnection(connectionString)) { List<T> list = con.Query<T>(proc, param, null, true, null, CommandType.StoredProcedure).ToList(); return list; } } } }
Dapper文件下载
https://files.cnblogs.com/files/huweizhong/Dapper.zip