标签:mpi 编码 table too oid exec odi for 自己
在项目开发中公共帮助类是必不可少的,这里记录一些自己摘录或自己编写的帮助类。
64位编码与解码:
#region URL的64位编码 /// <summary> /// URL的64位编码 /// </summary> /// <param name="sourthUrl"></param> /// <returns></returns> public static string Base64Encrypt(string sourthUrl) { string eurl = HttpUtility.UrlEncode(sourthUrl); eurl = Convert.ToBase64String(Encoding.Default.GetBytes(eurl)); return eurl; } #endregion #region URL的64位解码 /// <summary> /// URL的64位解码 /// </summary> /// <param name="eStr"></param> /// <returns></returns> public static string Base64Decrypt(string eStr) { if (!IsBase64(eStr)) { return eStr; } byte[] buffer = Convert.FromBase64String(eStr); string sourthUrl = Encoding.Default.GetString(buffer); sourthUrl = HttpUtility.UrlDecode(sourthUrl); return sourthUrl; } #endregion #region 检查一个字符串是否是Base64字符串 /// <summary> /// 检查一个字符串是否是Base64字符串 /// </summary> /// <param name="eStr"></param> /// <returns></returns> public static bool IsBase64(string eStr) { if ((eStr.Length % 4) != 0) { return false; } if (!Regex.IsMatch(eStr, "^[A-Z0-9/+=]*$", RegexOptions.IgnoreCase)) { return false; } return true; } #endregion
字符串过滤:
#region 检测是否有Sql危险字符 /// <summary> /// 检测是否有Sql危险字符 /// </summary> /// <param name="str">要判断字符串</param> /// <returns>判断结果</returns> public static bool IsSafeSqlString(string str) { return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\‘]"); } /// <summary> /// 检查危险字符 /// </summary> /// <param name="Input"></param> /// <returns></returns> public static string Filter(string sInput) { if (sInput == null || sInput == "") return null; string sInput1 = sInput.ToLower(); string output = sInput; string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|‘"; if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success) { throw new Exception("字符串中含有非法字符!"); } else { output = output.Replace("‘", "‘‘"); } return output; } /// <summary> /// 检查过滤设定的危险字符 /// </summary> /// <param name="InText">要过滤的字符串 </param> /// <returns>如果参数存在不安全字符,则返回true </returns> public static bool SqlFilter(string word, string InText) { if (InText == null) return false; foreach (string i in word.Split(‘|‘)) { if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1)) { return true; } } return false; } #endregion #region 过滤特殊字符 /// <summary> /// 过滤特殊字符 /// </summary> /// <param name="Input"></param> /// <returns></returns> public static string Htmls(string Input) { if (Input != string.Empty && Input != null) { string ihtml = Input.ToLower(); ihtml = ihtml.Replace("<script", "<script"); ihtml = ihtml.Replace("script>", "script>"); ihtml = ihtml.Replace("<%", "<%"); ihtml = ihtml.Replace("%>", "%>"); ihtml = ihtml.Replace("<$", "<$"); ihtml = ihtml.Replace("$>", "$>"); return ihtml; } else { return string.Empty; } } #endregion
字符串与List相互转化:
#region 把字符串按照分隔符转换成 List /// <summary> /// 把字符串按照分隔符转换成 List /// </summary> /// <param name="str">源字符串</param> /// <param name="speater">分隔符</param> /// <param name="toLower">是否转换为小写</param> /// <returns></returns> public static List<string> GetStrArray(string str, char speater, bool toLower) { List<string> list = new List<string>(); string[] ss = str.Split(speater); foreach (string s in ss) { if (!string.IsNullOrEmpty(s) && s != speater.ToString()) { string strVal = s; if (toLower) { strVal = s.ToLower(); } list.Add(strVal); } } return list; } #endregion #region 把 List<string> 按照分隔符组装成 string /// <summary> /// 把 List<string> 按照分隔符组装成 string /// </summary> /// <param name="list"></param> /// <param name="speater">分隔符</param> /// <returns></returns> public static string GetArrayStr(List<string> list, string speater) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Count; i++) { if (i == list.Count - 1) { sb.Append(list[i]); } else { sb.Append(list[i]); sb.Append(speater); } } return sb.ToString(); } #endregion #region 把 List<int> 按照分隔符组装成 string /// <summary> /// 把 List<int> 按照分隔符组装成 string /// </summary> /// <param name="list"></param> /// <param name="speater">分隔符</param> /// <returns></returns> public static string GetArrayStr(List<int> list, string speater) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Count; i++) { if (i == list.Count - 1) { sb.Append(list[i].ToString()); } else { sb.Append(list[i]); sb.Append(","); } } return sb.ToString(); } #endregion
HTML转成Text文本:
/// <summary> /// HTML转行成TEXT /// </summary> /// <param name="strHtml"></param> /// <returns></returns> public static string HtmlToTxt(string strHtml) { string[] aryReg ={ @"<script[^>]*?>.*?</script>", @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""‘])(\\[""‘tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", @"([\r\n])[\s]+", @"&(quot|#34);", @"&(amp|#38);", @"&(lt|#60);", @"&(gt|#62);", @"&(nbsp|#160);", @"&(iexcl|#161);", @"&(cent|#162);", @"&(pound|#163);", @"&(copy|#169);", @"&#(\d+);", @"-->", @"<!--.*\n" }; string newReg = aryReg[0]; string strOutput = strHtml; for (int i = 0; i < aryReg.Length; i++) { Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase); strOutput = regex.Replace(strOutput, string.Empty); } strOutput.Replace("<", ""); strOutput.Replace(">", ""); strOutput.Replace("\r\n", ""); return strOutput; }
DataTable转List<T>:
/// <summary> /// DataTable转换成List<T> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dt"></param> /// <param name="IsDataField"></param> /// <returns></returns> public static List<T> DataTableToList<T>(DataTable dt, bool IsDataField) where T : new() { List<T> ts = new List<T>(); Type type = typeof(T); string tempName = string.Empty; foreach (DataRow dr in dt.Rows) { T myt = new T(); PropertyInfo[] propertys = myt.GetType().GetProperties(); PropertyInfo[] array = propertys; int i = 0; DataFieldAttribute attribute = null; PropertyAttribute proAttribute = null; while (i < array.Length) { PropertyInfo pi = array[i]; if (IsDataField) { object[] infos = null; object[] pros = null; infos = pi.GetCustomAttributes(typeof(DataFieldAttribute), false); pros = pi.GetCustomAttributes(typeof(PropertyAttribute), false); if (infos.Length > 0) { attribute = (DataFieldAttribute)(infos[0]); if (pros.Length > 0) { proAttribute = (PropertyAttribute)(pros[0]); if (proAttribute.columnKeyType != ColumnKeyType.Extend) { tempName = attribute.FieldName; } } else tempName = attribute.FieldName; } } else tempName = pi.Name; if (dt.Columns.Contains(tempName)) { if (pi.CanWrite) { object value = dr[tempName]; //if (value.GetType().Equals(typeof(DateTime))) // value = System.Convert.ToString(value); if (value != DBNull.Value) pi.SetValue(myt, value, null); } } i += 1; continue; } ts.Add(myt); } return ts; }
DataTable 转 Object:
/// <summary> /// DataTable 转 Object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dt"></param> /// <returns></returns> public static T DataTableToObject<T>(DataTable dt, bool IsDataField) where T : new() { Type type = typeof(T); string tempName = string.Empty; T myt = new T(); PropertyInfo[] propertys = myt.GetType().GetProperties(); PropertyInfo[] array = propertys; int i = 0; DataFieldAttribute attribute = null; PropertyAttribute proAttribute = null; while (i < array.Length) { PropertyInfo pi = array[i]; if (IsDataField) { object[] infos = null; object[] pros = null; infos = pi.GetCustomAttributes(typeof(DataFieldAttribute), false); pros = pi.GetCustomAttributes(typeof(PropertyAttribute), false); if (infos.Length > 0) { attribute = (DataFieldAttribute)(infos[0]); if (pros.Length>0) { proAttribute = (PropertyAttribute)(pros[0]); if (proAttribute.columnKeyType != ColumnKeyType.Extend) { tempName = attribute.FieldName; } }else tempName = attribute.FieldName; } } else tempName = pi.Name; if (dt.Columns.Contains(tempName)) { if (pi.CanWrite) { object value = dt.Rows[0][tempName]; //if (value.GetType().Equals(typeof(DateTime))) // value = Convert.ToString(value); if (value != DBNull.Value) pi.SetValue(myt, value, null); } } i += 1; continue; } return myt; }
CacheHelper:
/// <summary> /// 获取数据缓存 /// </summary> /// <param name="CacheKey">键</param> public static object GetCache(string CacheKey) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey]; } /// <summary> /// 设置数据缓存 /// </summary> public static void SetCache(string CacheKey, object objObject) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject); } /// <summary> /// 设置数据缓存 /// </summary> public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null); } /// <summary> /// 移除指定数据缓存 /// </summary> public static void RemoveAllCache(string CacheKey) { System.Web.Caching.Cache _cache = HttpRuntime.Cache; _cache.Remove(CacheKey); } /// <summary> /// 移除全部缓存 /// </summary> public static void RemoveAllCache() { System.Web.Caching.Cache _cache = HttpRuntime.Cache; IDictionaryEnumerator CacheEnum = _cache.GetEnumerator(); while (CacheEnum.MoveNext()) { _cache.Remove(CacheEnum.Key.ToString()); } }
标签:mpi 编码 table too oid exec odi for 自己
原文地址:https://www.cnblogs.com/witeem/p/5594615.html