时间:2021-07-01 10:21:17 帮助过:16人阅读
1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Caching; 6 7 namespace Common 8 { 9 /// <summary> 10 /// 缓存帮助类 11 /// author:陈彦斌 12 /// 时间:2019年7月14日14:25:30 13 /// HttpRuntime.Cache 14 /// </summary> 15 public sealed class CacheHelper 16 { 17 /// <summary> 18 /// 获取configuratio节点下appSettings中add的值 19 /// </summary> 20 /// <param name="key">AppSettings的键</param> 21 /// <returns></returns> 22 public static object GetAppSettings(string key) 23 { 24 return ConfigurationManager.AppSettings[key]; 25 } 26 /// <summary> 27 /// 获取当前应用程序指定CacheKey的值 28 /// </summary> 29 /// <param name="CacheKey">appSettings节点下add中的键</param> 30 /// <returns></returns> 31 public static object GetCache(string CacheKey) 32 { 33 Cache objCache = HttpRuntime.Cache; 34 return objCache[CacheKey]; 35 } 36 /// <summary> 37 /// 设置数据缓存(慎用) 38 /// </summary> 39 /// <param name="CacheKey">键</param> 40 /// <param name="CacheValue">值</param> 41 public static void SetCache(string CacheKey,object CacheValue) 42 { 43 Cache objCache = HttpRuntime.Cache; 44 objCache.Insert(CacheKey, CacheValue); 45 } 46 /// <summary> 47 /// 设置数据缓存 48 /// </summary> 49 /// <param name="CacheKey">键</param> 50 /// <param name="CacheValue">值</param> 51 /// <param name="TimeOut">时间间隔</param> 52 public static void SetCache(string CacheKey, object CacheValue, TimeSpan TimeOut) 53 { 54 Cache objCache = HttpRuntime.Cache; 55 objCache.Insert(CacheKey, CacheValue, null, DateTime.MaxValue, TimeOut, CacheItemPriority.NotRemovable, null); 56 } 57 /// <summary> 58 /// 设置数据缓存 59 /// </summary> 60 /// <param name="CacheKey">键</param> 61 /// <param name="CacheValue">值</param> 62 /// <param name="absoluteExpiration">绝对过期时间</param> 63 /// <param name="slidingExpiration">时间间隔</param> 64 public static void SetCache(string CacheKey, object CacheValue, DateTime absoluteExpiration, TimeSpan slidingExpiration) 65 { 66 Cache objCache = HttpRuntime.Cache; 67 objCache.Insert(CacheKey, CacheValue, null, absoluteExpiration, slidingExpiration); 68 } 69 /// <summary> 70 /// 移除全部缓存 71 /// </summary> 72 public static void RemovaAllCache() 73 { 74 Cache objCache = HttpRuntime.Cache; 75 IDictionaryEnumerator CacheEnum = objCache.GetEnumerator(); 76 while (CacheEnum.MoveNext()) 77 { 78 objCache.Remove(CacheEnum.Key.ToString()); 79 } 80 } 81 /// <summary> 82 /// 移除指定键的缓存 83 /// </summary> 84 /// <param name="CacheKey">键</param> 85 public static void RemovaAllCache(string CacheKey) 86 { 87 Cache objCache = HttpRuntime.Cache; 88 objCache.Remove(CacheKey); 89 } 90 } 91 }
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.IO; 5 using System.Data.Common; 6 using System.Data; 7 using System.Data.SqlClient; 8 using System.Data.OracleClient; 9 using System.Threading; 10 11 namespace Tool 12 { 13 /// <summary> 14 /// 操作数据库基类 15 /// author:陈彦斌 16 /// 时间:2019年7月14日23:35:30 17 /// </summary> 18 public abstract class DbBaseTool 19 { 20 public const string c_where = " WHERE "; 21 public const string c_where_one_equal_one = " WHERE 1=1 "; 22 public const string c_where_one_equal_one_and = " WHERE 1=1 AND "; 23 public const string c_like = " LIKE "; 24 public const string c_and = " AND "; 25 public const string c_or = " OR "; 26 public const string c_equal = " = "; 27 public const char c_comma_split = ‘,‘; 28 } 29 /// <summary> 30 /// 错误信息打印类 31 /// author:陈彦斌 32 /// 时间:2019年7月14日23:36:10 33 /// </summary> 34 public class PrintSqlTool 35 { 36 public static string LogBasePath { get; set; } 37 public static Queue<string> execSqlQueue = new Queue<string>(); 38 private const string printTxtSqlTemp = "打印时间:{0}\r\nSQL语句:\r\n {1}\r\n"; 39 static PrintSqlTool() 40 { 41 ThreadPool.QueueUserWorkItem(o => 42 { 43 while (true) 44 { 45 lock (execSqlQueue) 46 { 47 if (execSqlQueue.Count > 0) 48 { 49 PrintSqlToText(LogBasePath,execSqlQueue.Dequeue()); 50 } 51 } 52 } 53 }); 54 } 55 /// <summary> 56 /// 打印报错SQL语句 57 /// </summary> 58 /// <param name="strPath">物理绝对路径</param> 59 /// <param name="sql">报错SQL语句</param> 60 public static void PrintSqlToText(string strPath, string sql) 61 { 62 appendStrToTxtFile(strPath, sql); 63 } 64 /// <summary> 65 /// 打印报错SQL语句 66 /// </summary> 67 /// <param name="strPath">物理绝对路径</param> 68 /// <param name="list">报错SQL语句集合</param> 69 public static void PrintSqlToText(string strPath, List<string> list) 70 { 71 StringBuilder sb = new StringBuilder(); 72 foreach (var item in list) 73 { 74 sb.Append(item).Append(";"); 75 } 76 appendStrToTxtFile(strPath, sb.ToString().TrimEnd(‘,‘)); 77 } 78 /// <summary> 79 /// 向文本追加字符串 80 /// </summary> 81 /// <param name="fileFullPath">物理绝对路径</param> 82 /// <param name="errStr">报错语句</param> 83 private static void appendStrToTxtFile(string fileFullPath, string errStr) 84 { 85 FileStream fs = null; //文件流 86 StreamWriter sw = null; //写入流 87 try 88 { 89 if (File.Exists(fileFullPath)) //判断文件是否存在 90 { 91 fs = new FileStream(fileFullPath, FileMode.Append); //打开文件搜寻到文件尾 92 } 93 else 94 { 95 fs = new FileStream(fileFullPath, FileMode.Create); //创建文件 96 } 97 sw = new StreamWriter(fs, Encoding.UTF8); //指定写入格式 98 sw.Write(string.Format(printTxtSqlTemp,DateTime.Now.ToString(), errStr)); //写入 99 } 100 catch (UnauthorizedAccessException err) 101 { 102 throw err; 103 } 104 catch (IOException err) 105 { 106 throw err; 107 } 108 finally 109 { 110 if (sw != null) 111 { 112 sw.Close(); 113 } 114 if (fs != null) 115 { 116 fs.Close(); 117 } 118 } 119 } 120 } 121 /// <summary> 122 /// 数据接口类 123 /// author:陈彦斌 124 /// 时间:2019年7月14日23:36:51 125 /// </summary> 126 public interface IDbProvider 127 { 128 /// <summary> 129 /// 连接字符串 130 /// </summary> 131 string connStr { get; set; } 132 /// <summary> 133 /// 初始化 IDbConnection 类的新实例。 134 /// </summary> 135 /// <returns></returns> 136 IDbConnection GetConnection(); 137 /// <summary> 138 /// 如果给定包含连接字符串的字符串,则初始化 IDbConnection 类的新实例。 139 /// </summary> 140 /// <param name="connectionString">用于打开 SQL Server 数据库的连接。</param> 141 /// <returns></returns> 142 IDbConnection GetConnection(string connectionString); 143 /// <summary> 144 /// 初始化 IDbCommand 类的新实例。 145 /// </summary> 146 /// <returns></returns> 147 IDbCommand GetCommand(); 148 /// <summary> 149 /// 用查询文本初始化 IDbCommand 类的新实例。 150 /// </summary> 151 /// <param name="cmdText">查询的文本。</param> 152 /// <returns></returns> 153 IDbCommand GetCommand(string cmdText); 154 /// <summary> 155 /// 初始化 IDbCommand 类的新实例。 156 /// </summary> 157 /// <param name="connection">数据库链接字符串</param> 158 /// <param name="transaction">将在其中执行 IDbCommand 的 IDbTransaction。</param> 159 /// <returns></returns> 160 IDbCommand GetCommand(IDbConnection connection, IDbTransaction transaction); 161 /// <summary> 162 /// 初始化具有查询文本和 IDbConnection 的 IDbCommand类的新实例。 163 /// </summary> 164 /// <param name="cmdText">查询的文本。</param> 165 /// <param name="connection">