123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- 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 "";
- }
- /// <summary>
- /// 单引号转@
- /// </summary>
- /// <param name="pcStr"></param>
- /// <returns></returns>
- 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 "";
- }
- /// <summary>
- /// 将string型转换成 Int 型
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static int ValI(string str)
- {
- return (int)ValD(str);
- }
- /// <summary>
- /// 将string型转换成 Int64 型
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static Int64 ValI64(string str)
- {
- return (Int64)ValD(str);
- }
- /// <summary>
- /// 字符串转换成数值型
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值.
- /// </summary>
- /// <param name="DateTime1">第一个日期和时间</param>
- /// <param name="DateTime2">第二个日期和时间</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 已重载.计算一个时间与当前本地日期和时间的时间间隔,返回的是时间间隔的日期差的绝对值.
- /// </summary>
- /// <param name="DateTime1">一个日期和时间</param>
- /// <returns></returns>
- 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
- }
- }
|