1、读取XML文件
1 /// <summary> 2 /// 读取XML配置文件类 3 /// </summary> 4 public class XmlHelper 5 { 6 private string strXmlPath = ""; //Xml文档路径 7 private XmlDocument xmlDoc; //XML文档 8 9 /// <summary> 10 /// 初始化ReadXml类 11 /// </summary> 12 /// <param name="XMLPath">XML文件路径</param> 13 public XmlHelper(string XMLPath) 14 { 15 if (!System.IO.File.Exists(XMLPath)) 16 { 17 18 throw new Exception("没有找到指定的路径:" + XMLPath + "的XML文档"); 19 } 20 21 strXmlPath = XMLPath; 22 xmlDoc = new XmlDocument(); 23 xmlDoc.Load(XMLPath); 24 } 25 26 /// <summary> 27 /// 读取XML文件指定键值的value值 28 /// </summary> 29 /// <param name="XMLNodePath">键值的路径,格式为(根节点/节点/子节点)</param> 30 /// <param name="valueName">指定键值的属性名称</param> 31 /// <returns>value值</returns> 32 public string ReadXmlValue(string XMLNodePath,string valueName) 33 { 34 try 35 { 36 XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XMLNodePath); 37 return xml.GetAttribute(valueName); 38 } 39 catch (Exception ex) 40 { 41 throw new Exception(ex.Message); 42 } 43 } 44 45 /// <summary> 46 /// 写XML指定节点的 47 /// </summary> 48 /// <param name="XmlNodePath">键值路径,格式为((根节点/节点/子节点))</param> 49 /// <param name="valueName">属性名称</param> 50 /// <param name="Value">属性</param> 51 /// <returns></returns> 52 public bool WriteXmlValue(string XmlNodePath,string valueName, string Value) 53 { 54 try 55 { 56 XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath); 57 xml.SetAttribute(valueName, Value); //设置 58 xmlDoc.Save(strXmlPath); //保存 59 return true; 60 } 61 catch (Exception ex) 62 { 63 throw new Exception(ex.Message); 64 } 65 } 66 67 /// <summary> 68 /// 读取XML键值 69 /// </summary> 70 /// <param name="XmlNodePath">键值路径,格式为((根节点/节点/子节点))</param> 71 /// <returns></returns> 72 public string ReadXmlKey(string XmlNodePath) 73 { 74 try 75 { 76 XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath); 77 return xml.InnerText; 78 } 79 catch (Exception ex) 80 { 81 throw new Exception(ex.Message); 82 } 83 } 84 85 /// <summary> 86 /// 写XML键值 87 /// </summary> 88 /// <param name="XmlNodePath">键值路径,格式为((根节点/节点/子节点))</param> 89 /// <param name="Value">值</param> 90 /// <returns></returns> 91 public bool WriteXmlKey(string XmlNodePath, string Value) 92 { 93 try 94 { 95 XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath); 96 xml.InnerText=Value; 97 xmlDoc.Save(strXmlPath); 98 return true; 99 } 100 catch (Exception ex) 101 { 102 throw new Exception(ex.Message); 103 } 104 } 105 }
1 public static class XmlToEgg<T> where T : class 2 { 3 private static string path; 4 private static T target; 5 6 static XmlToEgg() 7 { 8 } 9 /// <summary> 10 /// Sets the xml path. 11 /// </summary> 12 public static void SetXmlPath(string p) 13 { 14 path = p; 15 } 16 /// <summary> 17 /// Loads the XML Files. 18 /// </summary> 19 private static XElement LoadXML() 20 { 21 if(path == null) 22 return null; 23 XElement xml = XElement.Load(path); 24 return xml; 25 } 26 /// <summary> 27 /// Creates the class initiate. 28 /// </summary> 29 private static void CreateInitiate() 30 { 31 Type t = typeof(T); 32 ConstructorInfo ct = t.GetConstructor(System.Type.EmptyTypes); 33 target = (T)ct.Invoke(null); 34 } 35 /// <summary> 36 /// attribute assignment, 37 /// 由于反射中设置字段值的方法会涉及到赋值的目标类型和当前类型的转化, 38 /// 所以需要使用Convert.ChangeType进行类型转化 39 /// </summary> 40 public static T ToEgg() 41 { 42 if(target != null) 43 { 44 target = null; 45 } 46 CreateInitiate(); 47 XElement xml = LoadXML(); 48 Type t = target.GetType(); 49 FieldInfo[] fields = t.GetFields(); 50 string fieldName = string.Empty; 51 foreach(FieldInfo f in fields) 52 { 53 fieldName = f.Name; 54 if(xml.Element(fieldName) != null) 55 { 56 f.SetValue(target, Convert.ChangeType(xml.Element(fieldName).Value, f.FieldType)); 57 } 58 } 59 return target; 60 } 61 }
2、发送邮件
1 public class EmailHelper 2 { 3 public MailMessage mailMessage(string fromAddress,string desemailuser, string toAddress, string emailSubject, string emailText) //邮件的发送收者,支持群发,多个地址之间用 半角逗号 分开 4 { 5 MailMessage mm = new MailMessage(); 6 mm.To.Add("abc@qq.com"); 7 //mm.To.Add(toAddress);//暂时屏蔽 8 ////抄送 9 mm.CC.Add(new MailAddress("11@qq.com", "张三", Encoding.GetEncoding(936))); 10 mm.CC.Add(new MailAddress("22@qq.com", "李四", Encoding.GetEncoding(936))); 11 mm.CC.Add(new MailAddress("33@126.com", "王五", Encoding.GetEncoding(936))); 12 //mm.Bcc.Add("44@qq.com,44@qq.com"); 13 14 mm.From = new MailAddress(fromAddress, desemailuser.Length>0?desemailuser:fromAddress, Encoding.GetEncoding(936)); 15 mm.SubjectEncoding = Encoding.GetEncoding(936);//这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。 16 mm.Subject = emailSubject; //邮件标题 17 mm.IsBodyHtml = true; //邮件正文是否是HTML格式 18 19 mm.BodyEncoding = Encoding.GetEncoding(936); //邮件正文的编码, 设置不正确, 接收者会收到乱码 20 //------------------------------------------------------------------------- 21 emailText += "这是要正确发送的邮箱:" + toAddress; 22 mm.Body = emailText;//邮件正文 23 mm.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可 24 //mm.Attachments.Add(new Attachment(@"d:a.doc",System.Net.Mime.MediaTypeNames.Application.Rtf)); 25 ////第二个参数,表示附件的文件类型,可以不用指定 26 return mm; 27 } 28 29 public bool sendEmail(string fromemailaddress, string toemailaddress,string emailtitle,string emailcontent,string host,string port,string fromemailuser,string desemailuser,string fromemailpass) 30 { 31 bool b=false; 32 string message=string.Empty; 33 MailMessage mailessage =this.mailMessage(fromemailaddress,desemailuser,toemailaddress,emailtitle,emailcontent); 34 SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient 35 smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network 36 smtp.EnableSsl = false;//smtp服务器是否启用SSL加密 37 smtp.Host = host; //指定 smtp 服务器地址 38 smtp.Port = int.Parse(port); //指定 smtp 服务器的端口,默认是25 39 smtp.Credentials = new NetworkCredential(fromemailuser, fromemailpass);//认证 40 try 41 { 42 smtp.Send(mailessage); 43 b = True; 44 message= "发送成功!"; 45 } 46 catch (System.Net.Mail.SmtpException ex) 47 { 48 b = False; 49 message= "发送失败!"; 50 } 51 return b; 52 } 53 54 }
3、数据库读取
oracle
1 public class OraclHelper 2 { 3 4 public OleDbTransaction MyTransaction; 5 public OleDbConnection MyConnection; 6 public OleDbCommand MyCommand; 7 //数据库连接关键字 8 public readonly string strOledbCon; 9 public OraclHelper() 10 { 11 //public static readonly string connString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; 12 //System.Configuration.ConfigurationManager.AppSettings["EmailFrom"] 13 strOledbCon = System.Configuration.ConfigurationManager.AppSettings["DBConnectionString"]; 14 MyConnection = new OleDbConnection(strOledbCon); 15 } 16 public OraclHelper(string as_ConnectString) 17 { 18 strOledbCon = System.Configuration.ConfigurationManager.AppSettings[as_ConnectString]; 19 MyConnection = new OleDbConnection(strOledbCon); 20 } 21 22 /// <summary> 23 /// 读取数据表 24 /// </summary> 25 /// <param name="as_sqlstr">sql语句</param> 26 /// <returns></returns> 27 public OleDbDataReader GetDR(string as_sqlstr) 28 { 29 as_sqlstr = ChangeSQLStatement(as_sqlstr); 30 31 MyCommand = new OleDbCommand(as_sqlstr, MyConnection); 32 33 MyCommand.CommandTimeout = 600; 34 35 MyConnection.Open(); 36 OleDbDataReader dr = MyCommand.ExecuteReader(); 37 return dr; 38 } 39 40 /// <summary> 41 /// 获取数据视图 42 /// </summary> 43 /// <param name="as_sqlstr"></param> 44 /// <returns></returns> 45 public DataView GetDV(string as_sqlstr) 46 { 47 as_sqlstr = ChangeSQLStatement(as_sqlstr); 48 49 OleDbDataAdapter MyDA = new OleDbDataAdapter(as_sqlstr, MyConnection); 50 51 MyDA.SelectCommand.CommandTimeout = 600; 52 53 DataSet ds = new DataSet(); 54 MyDA.Fill(ds, "Result"); 55 DataView dv = ds.Tables["Result"].DefaultView; 56 57 MyDA.SelectCommand.Connection.Close(); 58 59 return dv; 60 } 61 62 /// <summary> 63 /// 获取数据表 64 /// </summary> 65 /// <param name="as_sqlstr"></param> 66 /// <returns></returns> 67 public DataTable GetDT(string as_sqlstr) 68 { 69 as_sqlstr = ChangeSQLStatement(as_sqlstr); 70 71 OleDbDataAdapter MyDA = new OleDbDataAdapter(as_sqlstr, MyConnection); 72 73 MyDA.SelectCommand.CommandTimeout = 600; 74 75 DataSet ds = new DataSet(); 76 MyDA.Fill(ds, "Result"); 77 DataTable dv = ds.Tables["Result"]; 78 79 MyDA.SelectCommand.Connection.Close(); 80 81 return dv; 82 } 83 84 /// <summary> 85 /// 获取数据表集 86 /// </summary> 87 /// <param name="as_sqlstr"></param> 88 /// <returns></returns> 89 public DataSet GetDT(string as_sqlstr) 90 { 91 as_sqlstr = ChangeSQLStatement(as_sqlstr); 92 93 OleDbDataAdapter MyDA = new OleDbDataAdapter(as_sqlstr, MyConnection); 94 95 MyDA.SelectCommand.CommandTimeout = 600; 96 97 DataSet ds = new DataSet(); 98 MyDA.Fill(ds, "Result"); 99 100 MyDA.SelectCommand.Connection.Close(); 101 return ds; 102 } 103 104 /// <summary> 105 /// 执行存储过程 106 /// </summary> 107 /// <param name="as_StoreProcedureName">存储过程名称</param> 108 /// <param name="param">参数</param> 109 /// <returns></returns> 110 public bool ExecStoreProcedure(string as_StoreProcedureName, ref OleDbParameter[] param) 111 { 112 bool lbl_rtn; 113 114 try 115 { 116 MyCommand = new OleDbCommand(as_StoreProcedureName, MyConnection); 117 MyCommand.CommandType = CommandType.StoredProcedure; 118 119 if (param != null) 120 { 121 for (int i = 0; i < param.Length; i++) 122 { 123 MyCommand.Parameters.Add(param[i]); 124 } 125 } 126 127 MyConnection.Open(); 128 129 MyCommand.ExecuteNonQuery(); 130 131 MyConnection.Close(); 132 133 lbl_rtn = true; 134 135 } 136 catch (Exception ex) 137 { 138 MyConnection.Close(); 139 lbl_rtn = false; 140 } 141 142 return lbl_rtn; 143 } 144 145 146 /// <summary> 147 /// 获得存储过程返回结果集 148 /// </summary> 149 /// <param name="as_StoreProcedureName">存储过程名称</param> 150 /// <param name="inParam">参数</param> 151 /// <returns></returns> 152 public DataTable GetDTFromStoreProcedure(string as_StoreProcedureName, OleDbParameter[] inParam) 153 { 154 DataTable dt = new DataTable(); 155 156 try 157 { 158 MyCommand = new OleDbCommand(as_StoreProcedureName, MyConnection); 159 MyCommand.CommandType = CommandType.StoredProcedure; 160 161 if (inParam != null) 162 { 163 for (int i = 0; i < inParam.Length; i++) 164 { 165 MyCommand.Parameters.Add(inParam[i]); 166 } 167 } 168 169 170 OleDbDataAdapter MyDA = new OleDbDataAdapter(MyCommand); 171 MyDA.Fill(dt); 172 } 173 catch (Exception) 174 { 175 176 } 177 178 return dt; 179 } 180 181 /// <summary> 182 /// 事务 183 /// </summary> 184 /// <param name="as_sqlstr"></param> 185 /// <param name="as_ErrMsg"></param> 186 /// <returns></returns> 187 public bool UpdateData(string as_sqlstr, ref string as_ErrMsg) 188 { 189 190 bool lbl_rtn; 191 192 as_ErrMsg = ""; 193 194 as_sqlstr = ChangeSQLStatement(as_sqlstr); 195 196 MyCommand = new OleDbCommand(); 197 MyCommand.Connection = MyConnection; 198 MyCommand.Transaction = MyTransaction; 199 200 try 201 { 202 MyCommand.CommandText = as_sqlstr; 203 MyCommand.ExecuteNonQuery(); 204 CommitData(); 205 lbl_rtn = true; 206 } 207 catch (Exception ex) 208 { 209 RollbackData(); 210 lbl_rtn = false; 211 as_ErrMsg = ex.Message; 212 213 } 214 finally 215 { 216 MyCommand.Dispose(); 217 } 218 219 return lbl_rtn; 220 } 221 222 /// <summary> 223 /// 关闭连接 224 /// </summary> 225 /// <param name="dr"></param 226 public void CloseDR(OleDbDataReader dr) 227 { 228 dr.Close(); 229 MyConnection.Close(); 230 } 231 232 public void OpenConnection() 233 { 234 MyConnection.Open(); 235 } 236 237 public void StartTransaction() 238 { 239 MyTransaction = MyConnection.BeginTransaction(IsolationLevel.ReadCommitted); 240 } 241 242 /// <summary> 243 /// 事务提交 244 /// </summary> 245 public void CommitData() 246 { 247 MyTransaction.Commit(); 248 MyConnection.Close(); 249 } 250 251 /// <summary> 252 /// 回滚数据 253 /// </summary> 254 public void RollbackData() 255 { 256 MyTransaction.Rollback(); 257 MyConnection.Close(); 258 } 259 /// <summary> 260 /// 字符替换 261 /// </summary> 262 /// <param name="as_sqlstr"></param> 263 /// <returns></returns> 264 public string ChangeSQLStatement(string as_sqlstr) 265 { 266 string ls_newSqlStr; 267 268 ls_newSqlStr = as_sqlstr; 269 270 if (strOledbCon.ToUpper().IndexOf("SQLOLEDB") >= 0) 271 { 272 ls_newSqlStr = ls_newSqlStr.Replace("sysdate", "getdate()"); 273 ls_newSqlStr = ls_newSqlStr.Replace("nvl(", "isnull("); 274 ls_newSqlStr = ls_newSqlStr.Replace("to_char(", "convert(varchar,"); 275 ls_newSqlStr = ls_newSqlStr.Replace("to_date(", "convert(date,"); 276 ls_newSqlStr = ls_newSqlStr.Replace("to_number(", "convert(numeric,"); 277 ls_newSqlStr = ls_newSqlStr.Replace("substr(", "substring("); 278 ls_newSqlStr = ls_newSqlStr.Replace("||", "+"); 279 ls_newSqlStr = ls_newSqlStr.Replace("length(", "len("); 280 ls_newSqlStr = ls_newSqlStr.Replace("rownum", "row_number() over(order by getdate())"); 281 ls_newSqlStr = ls_newSqlStr.Replace(", 'yyyy-MM-dd'", ""); 282 ls_newSqlStr = ls_newSqlStr.Replace(", 'yyyy-mm-dd'", ""); 283 ls_newSqlStr = ls_newSqlStr.Replace(",'yyyy-MM-dd'", ""); 284 ls_newSqlStr = ls_newSqlStr.Replace(",'yyyy-mm-dd'", ""); 285 ls_newSqlStr = ls_newSqlStr.Replace("'HH24:mm:SS'", "108"); 286 ls_newSqlStr = ls_newSqlStr.Replace("lineno,", ""lineno","); 287 ls_newSqlStr = ls_newSqlStr.Replace("lineno=", ""lineno"="); 288 ls_newSqlStr = ls_newSqlStr.Replace(" lineno ", " "lineno" "); 289 } 290 291 292 return ls_newSqlStr; 293 } 294 /// <summary> 295 /// 获得用户名 296 /// </summary> 297 /// <param name="as_staffcode"></param> 298 /// <returns></returns> 299 300 public string GetStaffName(string vip_id) 301 { 302 string ls_StaffName = ""; 303 304 OleDbDataReader dr = GetDR("Select staffname from staff where staffcode = '" + vip_id + "'"); 305 306 if (dr.Read()) 307 { 308 ls_StaffName = dr[0].ToString(); 309 310 CloseDR(dr); 311 return ls_StaffName; 312 313 } 314 else 315 { 316 CloseDR(dr); 317 return ls_StaffName; 318 } 319 } 320 }
4、缓存帮助类
1 /// <summary> 2 /// 服务器缓存帮助类 3 /// </summary> 4 public class CacheHelper 5 { 6 /// <summary> 7 /// 创建缓存项的文件依赖 8 /// </summary> 9 /// <param name="key">缓存Key</param> 10 /// <param name="obj">object对象</param> 11 /// <param name="fileName">文件绝对路径</param> 12 public static void InsertFile(string key, object obj, string fileName) 13 { 14 //创建缓存依赖项 15 CacheDependency dep = new CacheDependency(fileName); 16 //创建缓存 17 HttpRuntime.Cache.Insert(key, obj, dep); 18 } 19 20 /// <summary> 21 /// 创建缓存项过期 22 /// </summary> 23 /// <param name="key">缓存Key</param> 24 /// <param name="obj">object对象</param> 25 public static void Insert(string key, object obj) 26 { 27 if (obj != null) 28 { 29 if (IsExist(key)) 30 { 31 HttpRuntime.Cache[key] = obj; 32 } 33 else 34 { 35 int expires = ConvertHelper.ToInt(ConfigHelper.GetAppSettings("TimeCache")); 36 HttpRuntime.Cache.Insert(key, obj, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0)); 37 } 38 39 } 40 } 41 /// <summary> 42 /// 判断缓存对象是否存在 43 /// </summary> 44 /// <param name="strKey">缓存键值名称</param> 45 /// <returns>是否存在true 、false</returns> 46 public static bool IsExist(string strKey) 47 { 48 return HttpRuntime.Cache[strKey] != null; 49 } 50 /// <summary> 51 /// 获取缓存对象 52 /// </summary> 53 /// <param name="key">缓存Key</param> 54 /// <returns>object对象</returns> 55 public static object GetCache(string key) 56 { 57 if (string.IsNullOrEmpty(key)) 58 return null; 59 if (ConfigHelper.GetAppSettings("IsCache") == "false") 60 { 61 return null; 62 } 63 64 return HttpRuntime.Cache.Get(key); 65 } 66 67 /// <summary> 68 /// 获取缓存对象 69 /// </summary> 70 /// <typeparam name="T">T对象</typeparam> 71 /// <param name="key">缓存Key</param> 72 /// <returns></returns> 73 public static T Get<T>(string key) 74 { 75 object obj = GetCache(key); 76 return obj == null ? default(T) : (T)obj; 77 } 78 79 /// <summary> 80 /// 移除指定数据缓存 81 /// </summary> 82 public static void RemoveCache(string CacheKey) 83 { 84 System.Web.Caching.Cache _cache = HttpRuntime.Cache; 85 _cache.Remove(CacheKey); 86 } 87 88 /// <summary> 89 /// 移除全部缓存 90 /// </summary> 91 public static void RemoveAllCache() 92 { 93 System.Web.Caching.Cache _cache = HttpRuntime.Cache; 94 IDictionaryEnumerator CacheEnum = _cache.GetEnumerator(); 95 while (CacheEnum.MoveNext()) 96 { 97 _cache.Remove(CacheEnum.Key.ToString()); 98 } 99 } 100 }