brave helper

 

 

public  class DapperHelper 
    { 
        /// database connection name 
        Private  static  String the _connection = String .Empty; 

        /// Get the connection name         
        Private  static  String Connection 
        { 
            GET { return the _connection;}
             // SET = {the _connection value;} 
        } 

        /// Returns examples of connection         
        Private  static the IDbConnection dbConnection = null ; 

        /// static class instance variable storage         
        Private  static DapperHelper uniqueInstance;

        /// define a thread identifier to ensure synchronization         
        Private  static  Readonly  Object Locker = new new  Object ();
         ///  <Summary> 
        /// private constructor that creates instances of the class can not be outside, in order to achieve single-mode embodiment
         ///  </ Summary> 
        Private DapperHelper () 
        { 
            // for convenience the string to write directly demonstrate, sample project may be connected strings in a configuration file, and then read. 
            = the _connection @ " Server .; UID = SA =; pwd = sasasa; Database = Dapper " ; 
        } 

        ///  <Summary> 
        /// Gets an instance, where a single-mode embodiment, to ensure that there is only one instance
         ///  </ Summary> 
        ///  <Returns> </ Returns>
        public  static DapperHelper the GetInstance () 
        { 
            // double locked single-mode embodiment, plus a determination condition in the outer air mainly to reduce the lock, the lock release unnecessary loss 
            IF (uniqueInstance == null ) 
            { 
                Lock (Locker) 
                { 
                    IF (uniqueInstance == null ) 
                    { 
                        uniqueInstance = new new DapperHelper (); 
                    } 
                } 
            } 
            return uniqueInstance; 
        } 


        ///  <Summary> 
        /// to create and open a link database connection object
         /// </summary>
        /// <returns></returns>
        public static IDbConnection OpenCurrentDbConnection()
        {
            if (dbConnection == null)
            {
                dbConnection = new SqlConnection(Connection);
            }
            //判断连接状态
            if (dbConnection.State == ConnectionState.Closed)
            {
                dbConnection.Open();
            }
            return dbConnection;
        }
    }

DbContext

public  static  class the DbContext 
    { 
        // Get open database connectivity 
        Private  static the IDbConnection Db 
        { 
            GET 
            { 
                // create a single instance 
                DapperHelper.GetInstance ();
                 return DapperHelper.OpenCurrentDbConnection (); 
            } 
        } 

        ///  <Summary> 
        /// isolated an entity record
         ///  </ Summary> 
        ///  <typeParam name = "T"> </ typeParam> 
        ///  <param name = "SQL"> </ param> 
        ///  <Returns> </ Returns > 
        public  staticQueryFirstOrDefault T <T> ( String SQL, Object param = null ) 
        { 
            return Db.QueryFirstOrDefault <T> (SQL, param); 
        } 

        public  static the Task <T> QueryFirstOrDefaultAsync <T> ( String SQL, Object param = null ) 
        { 
            return Db.QueryFirstOrDefaultAsync <T> (SQL, param); 
        } 
        ///  <Summary> 
        /// isolated generic collection of multiple records entity
         ///  </ Summary> 
        ///  <typeParam name = "T"> generic T </ typeParam>
        /// <returns></returns>
        public static IEnumerable<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            return Db.Query<T>(sql, param, transaction, buffered, commandTimeout, commandType);
        }

        public static Task<IEnumerable<T>> QueryAsync<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return Db.QueryAsync<T>(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<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return Db.ExecuteScalar<T>(sql, param, transaction, commandTimeout, commandType);
        }

        public static Task<T> ExecuteScalarAsync<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return Db.ExecuteScalarAsync<T>(sql, param, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 同时查询多张表数据(高级查询)
        /// "select *from K_City;select *from K_Area";
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        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<SqlMapper.GridReader> QueryMultipleAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return Db.QueryMultipleAsync(sql, param, transaction, commandTimeout, commandType);
        }
    }

 

Guess you like

Origin www.cnblogs.com/siyunianhua/p/11493504.html