public class DapperHelper { /// 数据库连接名 private static string _connection = string.Empty; /// 获取连接名 private static string Connection { get { return _connection; } //set { _connection = value; } } /// 返回连接实例 private static IDbConnection dbConnection = null; /// 静态变量保存类的实例 private static DapperHelper uniqueInstance; /// 定义一个标识确保线程同步 private static readonly object locker = new object(); ////// 私有构造方法,使外界不能创建该类的实例,以便实现单例模式 /// private DapperHelper() { // 这里为了方便演示直接写的字符串,实例项目中可以将连接字符串放在配置文件中,再进行读取。 _connection = @"server=.;uid=sa;pwd=sasasa;database=Dapper"; } /// /// 获取实例,这里为单例模式,保证只存在一个实例 /// /// public static DapperHelper GetInstance() { // 双重锁定实现单例模式,在外层加个判空条件主要是为了减少加锁、释放锁的不必要的损耗 if (uniqueInstance == null) { lock (locker) { if (uniqueInstance == null) { uniqueInstance = new DapperHelper(); } } } return uniqueInstance; } /// /// 创建数据库连接对象并打开链接 /// /// public static IDbConnection OpenCurrentDbConnection() { if (dbConnection == null) { dbConnection = new SqlConnection(Connection); } //判断连接状态 if (dbConnection.State == ConnectionState.Closed) { dbConnection.Open(); } return dbConnection; } }
DbContext
public static class DbContext { // 获取开启数据库的连接 private static IDbConnection Db { get { //创建单一实例 DapperHelper.GetInstance(); return DapperHelper.OpenCurrentDbConnection(); } } ////// 查出一条记录的实体 /// /// /// /// public static T QueryFirstOrDefault (string sql, object param = null) { return Db.QueryFirstOrDefault (sql, param); } public static Task QueryFirstOrDefaultAsync (string sql, object param = null) { return Db.QueryFirstOrDefaultAsync (sql, param); } /// /// 查出多条记录的实体泛型集合 /// /// 泛型T /// public static IEnumerable Query (string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null) { return Db.Query (sql, param, transaction, buffered, commandTimeout, commandType); } public static Task > QueryAsync (string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.QueryAsync (sql, param, transaction, commandTimeout, commandType); } public static int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.Execute(sql, param, transaction, commandTimeout, commandType); } public static Task<int> ExecuteAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.ExecuteAsync(sql, param, transaction, commandTimeout, commandType); } public static T ExecuteScalar (string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.ExecuteScalar (sql, param, transaction, commandTimeout, commandType); } public static Task ExecuteScalarAsync (string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.ExecuteScalarAsync (sql, param, transaction, commandTimeout, commandType); } /// /// 同时查询多张表数据(高级查询) /// "select *from K_City;select *from K_Area"; /// /// /// public static SqlMapper.GridReader QueryMultiple(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.QueryMultiple(sql, param, transaction, commandTimeout, commandType); } public static Task QueryMultipleAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { return Db.QueryMultipleAsync(sql, param, transaction, commandTimeout, commandType); } }