Utils.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Drawing.Imaging;
  5. using System.Collections;
  6. using System.Drawing.Printing;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. using System.Web;
  10. namespace SysBaseLibs
  11. {
  12. public class Utils
  13. {
  14. public static string AreaToSQL(string pcStr)
  15. {
  16. if (pcStr != null)
  17. {
  18. return pcStr.Replace("'", "''");
  19. }
  20. return "";
  21. }
  22. public static string AreaToSQLcs(string pcStr)
  23. {
  24. if (pcStr != null)
  25. return pcStr.Trim().Replace("'", "''").Replace("\"", "''''");
  26. return "";
  27. }
  28. /// <summary>
  29. /// 单引号转@
  30. /// </summary>
  31. /// <param name="pcStr"></param>
  32. /// <returns></returns>
  33. public static string AreaToSqlTran(string pcStr)
  34. {
  35. if (pcStr != null)
  36. return pcStr.Trim().Replace("'", "@").Replace("\"", "''''");
  37. return "";
  38. }
  39. public static string CvFiedInSql(string inFieldName, string inField, string outField,string outFieldName)
  40. {
  41. string lcRetVal = "";
  42. Array Arr1 = UtilStr.StrToArray(inField);
  43. Array Arr2 = UtilStr.StrToArray(outField);
  44. if (outFieldName.Trim().Length == 0)
  45. {
  46. outFieldName = inFieldName;
  47. }
  48. if (Arr1.Length == Arr2.Length)
  49. {
  50. lcRetVal = " case " + inFieldName;
  51. for (int i = 0; i <= Arr1.Length; i++)
  52. {
  53. lcRetVal += " when '" + Arr1.GetValue(i).ToString() + "' then '" + Arr2.GetValue(i).ToString() + "' ";
  54. }
  55. lcRetVal += " end as " + outFieldName;
  56. }
  57. if (inFieldName == "" || inField == "" || outField == "")
  58. {
  59. lcRetVal = "";
  60. }
  61. return lcRetVal;
  62. }
  63. public static string Decode(string pcKey)
  64. {
  65. pcKey = pcKey.Replace("*", "%");
  66. pcKey = System.Web.HttpUtility.UrlDecode(pcKey, System.Text.Encoding.Default);
  67. return pcKey;
  68. }
  69. public static string Encode(string as_Key)
  70. {
  71. as_Key = System.Web.HttpUtility.UrlEncode(as_Key, System.Text.Encoding.Default);
  72. as_Key = as_Key.Replace("%", "*");
  73. return as_Key;
  74. }
  75. public static string IsHasSqlESC(string pcCheckedStr)
  76. {
  77. if (pcCheckedStr.Trim().Length > 0 && pcCheckedStr.IndexOf("'") > 0)
  78. {
  79. return "'";
  80. }
  81. return "N";
  82. }
  83. public static bool ObjToBool(object poObj)
  84. {
  85. if (poObj == null)
  86. return false;
  87. return UtilStr.StrToBool(UtilStr.StrFromObj(poObj));
  88. }
  89. public string SmallDate(ref string pcDateField, string pcShowName)
  90. {
  91. if (pcShowName.Trim().Length == 0)
  92. {
  93. pcShowName = pcDateField;
  94. }
  95. return ("(convert(char(10)," + pcDateField + ",120)) as " + pcShowName);
  96. }
  97. public static byte[] BytesFromImage(Image poImage)
  98. {
  99. byte[] buffer1 = null;
  100. if (poImage != null)
  101. {
  102. MemoryStream stream1 = new MemoryStream();
  103. poImage.Save(stream1, ImageFormat.Bmp);
  104. buffer1 = new byte[stream1.Length];
  105. stream1.Seek((long)0, SeekOrigin.Begin);
  106. stream1.Read(buffer1, 0, (int)stream1.Length);
  107. stream1.Close();
  108. }
  109. return buffer1;
  110. }
  111. public static object BytesToImage(object poObject)
  112. {
  113. object obj1 = null;
  114. if ((poObject != null) && !(poObject is DBNull))
  115. {
  116. try
  117. {
  118. byte[] buffer1 = (byte[])poObject;
  119. if (buffer1.Length > 0)
  120. {
  121. MemoryStream stream1 = new MemoryStream(buffer1, true);
  122. stream1.Write(buffer1, 0, buffer1.Length);
  123. obj1 = Image.FromStream(stream1);
  124. stream1.Close();
  125. }
  126. }
  127. catch (Exception exception1)
  128. {
  129. obj1 = exception1.Message;
  130. }
  131. return obj1;
  132. }
  133. return "";
  134. }
  135. /// <summary>
  136. /// 将string型转换成 Int 型
  137. /// </summary>
  138. /// <param name="str"></param>
  139. /// <returns></returns>
  140. public static int ValI(string str)
  141. {
  142. return (int)ValD(str);
  143. }
  144. /// <summary>
  145. /// 将string型转换成 Int64 型
  146. /// </summary>
  147. /// <param name="str"></param>
  148. /// <returns></returns>
  149. public static Int64 ValI64(string str)
  150. {
  151. return (Int64)ValD(str);
  152. }
  153. /// <summary>
  154. /// 字符串转换成数值型
  155. /// </summary>
  156. /// <param name="str"></param>
  157. /// <returns></returns>
  158. public static decimal ValD(string str)
  159. {
  160. string text1 = "";
  161. for (int num1 = 0; num1 < str.Length; num1++)
  162. {
  163. char ch1 = str[num1];
  164. switch (ch1)
  165. {
  166. case ' ':
  167. case ',':
  168. break;
  169. default:
  170. if ((ch1 >= '0') && (ch1 <= '9'))
  171. {
  172. text1 = text1 + ch1.ToString();
  173. }
  174. else if (((ch1 == '+') || (ch1 == '-')) && (text1 == ""))
  175. {
  176. text1 = text1 + ch1.ToString();
  177. }
  178. else
  179. {
  180. if ((ch1 != '.') || (text1.IndexOf(ch1) >= 0))
  181. {
  182. break;
  183. }
  184. text1 = text1 + ch1.ToString();
  185. }
  186. break;
  187. }
  188. }
  189. if (text1 == "")
  190. {
  191. text1 = "0";
  192. }
  193. else if (text1 == "-")
  194. {
  195. text1 = "-0";
  196. }
  197. return Convert.ToDecimal(text1);
  198. }
  199. public static decimal ValD(object poObj)
  200. {
  201. return ValD(UtilStr.StrFromObj(poObj));
  202. }
  203. public static float ValSingle(object pcStr)
  204. {
  205. float lfRetVal = 0f;
  206. try
  207. {
  208. lfRetVal = Convert.ToSingle(pcStr);
  209. }
  210. catch { }
  211. return lfRetVal;
  212. }
  213. public static float PixelsToInch(Graphics g, int pixels, bool horizontal)
  214. {
  215. float single1 = horizontal ? g.DpiX : g.DpiY;
  216. return (((float)pixels) / single1);
  217. }
  218. public static string FormatCodeToXML(string pcString)
  219. {
  220. return pcString.Replace("<", ";lt;").Replace(">", ";gt;").Replace("&", ";amp;");
  221. }
  222. public static string FormatXMLToCode(string pcString)
  223. {
  224. return pcString.Replace(";amp;", "&").Replace(";lt;", "<").Replace(";gt;", ">");
  225. }
  226. public static ArrayList ArrayToArrayList(Array paArr)
  227. {
  228. ArrayList list1 = new ArrayList();
  229. foreach (object obj1 in paArr)
  230. {
  231. list1.Add(obj1);
  232. }
  233. return list1;
  234. }
  235. public static bool IsNum(string pcStr)
  236. {
  237. for (int i = 0; i < pcStr.Length; i++)
  238. {
  239. if (pcStr[i] <= '0' || pcStr[i] >= '9')
  240. return false;
  241. }
  242. return true;
  243. }
  244. public static string ByteToString(byte[] InBytes)
  245. {
  246. string StringOut = "";
  247. foreach (byte InByte in InBytes)
  248. {
  249. StringOut = StringOut + String.Format("{0:X2} ", InByte);
  250. }
  251. return StringOut;
  252. }
  253. public static byte[] StringToByte(string InString)
  254. {
  255. string[] ByteStrings;
  256. ByteStrings = InString.Split(" ".ToCharArray());
  257. byte[] ByteOut;
  258. ByteOut = new byte[ByteStrings.Length - 1];
  259. for (int i = 0; i == ByteStrings.Length - 1; i++)
  260. {
  261. ByteOut[i] = Convert.ToByte(("0x" + ByteStrings[i]));
  262. }
  263. return ByteOut;
  264. }
  265. public static string GetQuerySQL(string pcFiled, string pcValue)
  266. {
  267. return GetQuerySQL(pcFiled, pcValue, false);
  268. }
  269. public static string GetQuerySQL(string pcFiled, string pcValue, bool pbAddAnd)
  270. {
  271. string lcRetVal = "";
  272. if (pcFiled.Trim().Length > 0)
  273. {
  274. if (pbAddAnd)
  275. lcRetVal = " and ";
  276. lcRetVal += " " + pcFiled + " like '%" + pcValue + "%'";
  277. }
  278. return lcRetVal;
  279. }
  280. /// <summary>
  281. /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值.
  282. /// </summary>
  283. /// <param name="DateTime1">第一个日期和时间</param>
  284. /// <param name="DateTime2">第二个日期和时间</param>
  285. /// <returns></returns>
  286. public static TimeSpan DateDiff(DateTime DateTime1, DateTime DateTime2)
  287. {
  288. TimeSpan dateDiff = new TimeSpan();
  289. try
  290. {
  291. TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
  292. TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
  293. dateDiff = ts1.Subtract(ts2).Duration();
  294. }
  295. catch
  296. {
  297. }
  298. return dateDiff;
  299. }
  300. /// <summary>
  301. /// 已重载.计算一个时间与当前本地日期和时间的时间间隔,返回的是时间间隔的日期差的绝对值.
  302. /// </summary>
  303. /// <param name="DateTime1">一个日期和时间</param>
  304. /// <returns></returns>
  305. public static TimeSpan DateDiff(DateTime DateTime1)
  306. {
  307. return DateDiff(DateTime1, DateTime.Now);
  308. }
  309. /**//// < summary>
  310. /// 分析用户请求是否正常
  311. /// < /summary>
  312. /// < param name="Str">传入用户提交数据< /param>
  313. /// < returns>返回是否含有SQL注入式攻击代码< /returns>
  314. public static bool ProcessSqlStr(string Str, int type=0)
  315. {
  316. string SqlStr;
  317. if (type == 1)
  318. {
  319. SqlStr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
  320. }
  321. else
  322. {
  323. SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";
  324. }
  325. bool ReturnValue = false;
  326. try
  327. {
  328. if (Str != "")
  329. {
  330. string[] anySqlStr = SqlStr.Split('|');
  331. foreach (string ss in anySqlStr)
  332. {
  333. if (Str.IndexOf(ss) >= 0)
  334. {
  335. ReturnValue = true;
  336. }
  337. }
  338. }
  339. }
  340. catch
  341. {
  342. ReturnValue = false;
  343. }
  344. return ReturnValue;
  345. }
  346. }
  347. public enum CovType
  348. {
  349. sqlQuary = 0,
  350. jsStr = 1,
  351. htmlStr = 2,
  352. CtrlText = 3,
  353. urlMsg = 4
  354. }
  355. }