using System; using System.Drawing; using System.IO; using System.Drawing.Imaging; using System.Collections; using System.Drawing.Printing; using System.Collections.Generic; using System.Text; using System.Web; namespace SysBaseLibs { public class Utils { public static string AreaToSQL(string pcStr) { if (pcStr != null) { return pcStr.Replace("'", "''"); } return ""; } public static string AreaToSQLcs(string pcStr) { if (pcStr != null) return pcStr.Trim().Replace("'", "''").Replace("\"", "''''"); return ""; } /// /// 单引号转@ /// /// /// public static string AreaToSqlTran(string pcStr) { if (pcStr != null) return pcStr.Trim().Replace("'", "@").Replace("\"", "''''"); return ""; } public static string CvFiedInSql(string inFieldName, string inField, string outField,string outFieldName) { string lcRetVal = ""; Array Arr1 = UtilStr.StrToArray(inField); Array Arr2 = UtilStr.StrToArray(outField); if (outFieldName.Trim().Length == 0) { outFieldName = inFieldName; } if (Arr1.Length == Arr2.Length) { lcRetVal = " case " + inFieldName; for (int i = 0; i <= Arr1.Length; i++) { lcRetVal += " when '" + Arr1.GetValue(i).ToString() + "' then '" + Arr2.GetValue(i).ToString() + "' "; } lcRetVal += " end as " + outFieldName; } if (inFieldName == "" || inField == "" || outField == "") { lcRetVal = ""; } return lcRetVal; } public static string Decode(string pcKey) { pcKey = pcKey.Replace("*", "%"); pcKey = System.Web.HttpUtility.UrlDecode(pcKey, System.Text.Encoding.Default); return pcKey; } public static string Encode(string as_Key) { as_Key = System.Web.HttpUtility.UrlEncode(as_Key, System.Text.Encoding.Default); as_Key = as_Key.Replace("%", "*"); return as_Key; } public static string IsHasSqlESC(string pcCheckedStr) { if (pcCheckedStr.Trim().Length > 0 && pcCheckedStr.IndexOf("'") > 0) { return "'"; } return "N"; } public static bool ObjToBool(object poObj) { if (poObj == null) return false; return UtilStr.StrToBool(UtilStr.StrFromObj(poObj)); } public string SmallDate(ref string pcDateField, string pcShowName) { if (pcShowName.Trim().Length == 0) { pcShowName = pcDateField; } return ("(convert(char(10)," + pcDateField + ",120)) as " + pcShowName); } public static byte[] BytesFromImage(Image poImage) { byte[] buffer1 = null; if (poImage != null) { MemoryStream stream1 = new MemoryStream(); poImage.Save(stream1, ImageFormat.Bmp); buffer1 = new byte[stream1.Length]; stream1.Seek((long)0, SeekOrigin.Begin); stream1.Read(buffer1, 0, (int)stream1.Length); stream1.Close(); } return buffer1; } public static object BytesToImage(object poObject) { object obj1 = null; if ((poObject != null) && !(poObject is DBNull)) { try { byte[] buffer1 = (byte[])poObject; if (buffer1.Length > 0) { MemoryStream stream1 = new MemoryStream(buffer1, true); stream1.Write(buffer1, 0, buffer1.Length); obj1 = Image.FromStream(stream1); stream1.Close(); } } catch (Exception exception1) { obj1 = exception1.Message; } return obj1; } return ""; } /// /// 将string型转换成 Int 型 /// /// /// public static int ValI(string str) { return (int)ValD(str); } /// /// 将string型转换成 Int64 型 /// /// /// public static Int64 ValI64(string str) { return (Int64)ValD(str); } /// /// 字符串转换成数值型 /// /// /// public static decimal ValD(string str) { string text1 = ""; for (int num1 = 0; num1 < str.Length; num1++) { char ch1 = str[num1]; switch (ch1) { case ' ': case ',': break; default: if ((ch1 >= '0') && (ch1 <= '9')) { text1 = text1 + ch1.ToString(); } else if (((ch1 == '+') || (ch1 == '-')) && (text1 == "")) { text1 = text1 + ch1.ToString(); } else { if ((ch1 != '.') || (text1.IndexOf(ch1) >= 0)) { break; } text1 = text1 + ch1.ToString(); } break; } } if (text1 == "") { text1 = "0"; } else if (text1 == "-") { text1 = "-0"; } return Convert.ToDecimal(text1); } public static decimal ValD(object poObj) { return ValD(UtilStr.StrFromObj(poObj)); } public static float ValSingle(object pcStr) { float lfRetVal = 0f; try { lfRetVal = Convert.ToSingle(pcStr); } catch { } return lfRetVal; } public static float PixelsToInch(Graphics g, int pixels, bool horizontal) { float single1 = horizontal ? g.DpiX : g.DpiY; return (((float)pixels) / single1); } public static string FormatCodeToXML(string pcString) { return pcString.Replace("<", ";lt;").Replace(">", ";gt;").Replace("&", ";amp;"); } public static string FormatXMLToCode(string pcString) { return pcString.Replace(";amp;", "&").Replace(";lt;", "<").Replace(";gt;", ">"); } public static ArrayList ArrayToArrayList(Array paArr) { ArrayList list1 = new ArrayList(); foreach (object obj1 in paArr) { list1.Add(obj1); } return list1; } public static bool IsNum(string pcStr) { for (int i = 0; i < pcStr.Length; i++) { if (pcStr[i] <= '0' || pcStr[i] >= '9') return false; } return true; } public static string ByteToString(byte[] InBytes) { string StringOut = ""; foreach (byte InByte in InBytes) { StringOut = StringOut + String.Format("{0:X2} ", InByte); } return StringOut; } public static byte[] StringToByte(string InString) { string[] ByteStrings; ByteStrings = InString.Split(" ".ToCharArray()); byte[] ByteOut; ByteOut = new byte[ByteStrings.Length - 1]; for (int i = 0; i == ByteStrings.Length - 1; i++) { ByteOut[i] = Convert.ToByte(("0x" + ByteStrings[i])); } return ByteOut; } public static string GetQuerySQL(string pcFiled, string pcValue) { return GetQuerySQL(pcFiled, pcValue, false); } public static string GetQuerySQL(string pcFiled, string pcValue, bool pbAddAnd) { string lcRetVal = ""; if (pcFiled.Trim().Length > 0) { if (pbAddAnd) lcRetVal = " and "; lcRetVal += " " + pcFiled + " like '%" + pcValue + "%'"; } return lcRetVal; } /// /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值. /// /// 第一个日期和时间 /// 第二个日期和时间 /// public static TimeSpan DateDiff(DateTime DateTime1, DateTime DateTime2) { TimeSpan dateDiff = new TimeSpan(); try { TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); dateDiff = ts1.Subtract(ts2).Duration(); } catch { } return dateDiff; } /// /// 已重载.计算一个时间与当前本地日期和时间的时间间隔,返回的是时间间隔的日期差的绝对值. /// /// 一个日期和时间 /// public static TimeSpan DateDiff(DateTime DateTime1) { return DateDiff(DateTime1, DateTime.Now); } /**//// < summary> /// 分析用户请求是否正常 /// < /summary> /// < param name="Str">传入用户提交数据< /param> /// < returns>返回是否含有SQL注入式攻击代码< /returns> public static bool ProcessSqlStr(string Str, int type=0) { string SqlStr; if (type == 1) { SqlStr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare "; } else { SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare"; } bool ReturnValue = false; try { if (Str != "") { string[] anySqlStr = SqlStr.Split('|'); foreach (string ss in anySqlStr) { if (Str.IndexOf(ss) >= 0) { ReturnValue = true; } } } } catch { ReturnValue = false; } return ReturnValue; } } public enum CovType { sqlQuary = 0, jsStr = 1, htmlStr = 2, CtrlText = 3, urlMsg = 4 } }