Ejemplo n.º 1
0
 /// <summary>
 /// 类的单例创建函数
 /// </summary>
 /// <param name="conStr"></param>
 /// <returns></returns>
 public static AccessDbHelper CreateInstance(string conStr)
 {
     // 当第一个线程运行到这里时,此时会对locker对象 "加锁",
     // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁
     // lock语句运行完之后(即线程运行完之后)会对该对象"解锁"
     // 双重锁定只需要一句判断就可以了
     if (_accessDbHelper == null)
     {
         lock (_locker)
         {
             // 如果类的实例不存在则创建,否则直接返回
             if (_accessDbHelper == null)
             {
                 _accessDbHelper = new AccessDbHelper();
             }
         }
     }
     _accessDbHelper.ConStr = conStr;
     GC.Collect(1);
     return(_accessDbHelper);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 执行一般sql语句,返回受影响的记录行数
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="prePms">变量的键值对集合</param>
        /// <returns>受影响的记录行数</returns>
        public int QueryInt(string sql, Dictionary <string, object> prePms = null)
        {
            SqlServerHelper _sqlServer = SqlServerHelper.CreateInstance(SqlConStr);
            int             i          = 0;

            while (i <= 2)
            {
                try
                {
                    return(_sqlServer.QueryInt(sql, prePms));
                }
                catch (Exception ex)
                {
                    i++;
                    Thread.Sleep(500);
                    continue;
                    throw ex;
                }
            }
            AccessDbHelper _accessSever = AccessDbHelper.CreateInstance(AccessConStr);

            return(_accessSever.QueryInt(sql, prePms));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 泛型方法,根据where条件查询对应泛型的表中记录,并返回泛型列表
        /// </summary>
        /// <typeparam name="T">泛型,Model中数据表的对应类</typeparam>
        /// <param name="where">where条件</param>
        /// <returns>泛型列表</returns>
        public List <T> QueryList <T>(string where = null)
        {
            SqlServerHelper _sqlServer = SqlServerHelper.CreateInstance(SqlConStr);
            int             i          = 0;

            while (i <= 2)
            {
                try
                {
                    return(_sqlServer.QueryList <T>(where));
                }
                catch (Exception exc)
                {
                    i++;
                    Thread.Sleep(500);
                    continue;
                    throw exc;
                }
            }
            AccessDbHelper _accessSever = AccessDbHelper.CreateInstance(AccessConStr);

            return(_accessSever.QueryList <T>(where));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 泛型方法,根据对应操作和泛型列表执行对应数据库操作,返回对应操作影响的行数
        /// </summary>
        /// <typeparam name="T">泛型,Model中数据表的对应类</typeparam>
        /// <param name="opration">数据操作:Insert、Update、Delete忽略大小写</param>
        /// <param name="modelList">泛型列表</param>
        /// <returns>影响的行数</returns>
        public int QueryInt <T>(string opration, List <T> modelList)
        {
            SqlServerHelper _sqlServer = SqlServerHelper.CreateInstance(SqlConStr);
            int             i          = 0;

            while (i <= 2)
            {
                try
                {
                    return(_sqlServer.QueryInt <T>(opration, modelList));
                }
                catch (Exception exc)
                {
                    i++;
                    Thread.Sleep(500);
                    continue;
                    throw exc;
                }
            }
            AccessDbHelper _accessSever = AccessDbHelper.CreateInstance(AccessConStr);

            return(_accessSever.QueryInt <T>(opration, modelList));
        }