当前位置:Gxlcms > 数据库问题 > 面向对象编程、模块内高内聚、模块间低耦合、数据库操作工具类

面向对象编程、模块内高内聚、模块间低耦合、数据库操作工具类

时间:2021-07-01 10:21:17 帮助过:16人阅读

using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Reflection; 5 6 namespace Common 7 { 8 /// <summary> 9 /// web.config操作类 10 /// author:陈彦斌 11 /// 时间:2019年7月14日23:32:08 12 /// 使用前需引用程序集:System.configuration 13 /// </summary> 14 public sealed class ConfigHelper 15 { 16 /// <summary> 17 /// 获取系统配置信息 18 /// </summary> 19 /// <typeparam name="SystemConfig"></typeparam> 20 /// <returns></returns> 21 public static SystemConfig GetAppSettingsAllInfo() 22 { 23 try 24 { 25 SystemConfig t = new SystemConfig(); 26 string[] arrSysCfg = ConfigurationManager.AppSettings.AllKeys; 27 string value = string.Empty; 28 foreach (var key in arrSysCfg) 29 { 30 value = CacheHelper.GetAppSettings(key).ToString(); 31 foreach (PropertyInfo pi in t.GetType().GetProperties()) 32 { 33 if (key.Contains(pi.Name)) 34 { 35 if (!StringUtil.isNullOrBlank(value)) 36 { 37 pi.SetValue(t, value, null); 38 } 39 } 40 } 41 } 42 return t; 43 } 44 catch(Exception ex) 45 { 46 throw ex; 47 } 48 } 49 /// <summary> 50 /// 获取链接字符串 51 /// </summary> 52 /// <param name="key"></param> 53 /// <returns></returns> 54 public static string GetConnectionString(string key) 55 { 56 return ConfigurationManager.ConnectionStrings[key].ConnectionString; 57 } 58 /// <summary> 59 /// 获取AppSettings中配置String信息 60 /// </summary> 61 /// <param name="key"></param> 62 /// <returns></returns> 63 public static string GetConfigString(string key) 64 { 65 object objValue = CacheHelper.GetCache(key); 66 if (objValue == null) //缓冲区没有值 67 { 68 objValue = CacheHelper.GetAppSettings(key); 69 if (objValue != null) 70 { 71 CacheHelper.SetCache(key, objValue, DateTime.Now.AddMinutes(180), TimeSpan.Zero); 72 } 73 } 74 return objValue.ToString(); 75 } 76 /// <summary> 77 /// 获取AppSettings中配置Bool信息 78 /// </summary> 79 /// <param name="key"></param> 80 /// <returns></returns> 81 public static bool GetConfigBool(string key) 82 { 83 object objValue= CacheHelper.GetAppSettings(key); 84 if (StringUtil.isNullOrBlank(objValue)) 85 { 86 try 87 { 88 bool.Parse(objValue.ToString()); 89 return true; 90 } 91 catch 92 { 93 return false; 94 } 95 } 96 return false; 97 } 98 /// <summary> 99 /// 获取AppSettings中配置decimal信息 100 /// </summary> 101 /// <param name="key"></param> 102 /// <returns></returns> 103 public static decimal GetConfigDecimal(string key) 104 { 105 object objValue = CacheHelper.GetAppSettings(key); 106 if (StringUtil.isNullOrBlank(objValue)) 107 { 108 try 109 { 110 return decimal.Parse(objValue.ToString()); 111 } 112 catch 113 { 114 return 0; 115 } 116 } 117 return 0; 118 } 119 /// <summary> 120 /// 获取AppSettings中配置DateTime信息,可空 121 /// </summary> 122 /// <param name="key"></param> 123 /// <returns></returns> 124 public static DateTime? GetConfigDateTime(string key) 125 { 126 DateTime? DateTimeNull = null; 127 object objValue = CacheHelper.GetAppSettings(key); 128 if (StringUtil.isNullOrBlank(objValue)) 129 { 130 try 131 { 132 return DateTime.Parse(objValue.ToString()); 133 } 134 catch 135 { 136 return DateTimeNull; 137 } 138 } 139 return DateTimeNull; 140 } 141 } 142 /// <summary> 143 /// 系统配置类 144 /// </summary> 145 public sealed class SystemConfig 146 { 147 /// <summary> 148 /// 数据库连接字符串 149 /// </summary> 150 public string ConnectionString { get; set; } 151 /// <summary> 152 /// 数据库类型 153 /// </summary> 154 public string dbType { get; set; } 155 /// <summary> 156 /// 打印报错SQL语句物理路径 157 /// </summary> 158 public string PrintErrorSqlPath { get; set; } 159 /// <summary> 160 /// 是否打印 161 /// </summary> 162 public string IsPrint { get; set; } 163 } 164 }

二、缓存帮助类(CacheHelper.cs)

 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 }

三、数据库基类(DbBaseTool.cs)

  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">
                        
                    

人气教程排行