You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.4 KiB

2 years ago
  1. using System;
  2. namespace SqlSugar
  3. {
  4. public static class UtilConvert
  5. {
  6. public static int ObjToInt(this object thisValue)
  7. {
  8. var reval = 0;
  9. if (thisValue == null) return 0;
  10. if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
  11. {
  12. return reval;
  13. }
  14. return reval;
  15. }
  16. public static int ObjToInt(this object thisValue, int errorValue)
  17. {
  18. var reval = 0;
  19. if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
  20. {
  21. return reval;
  22. }
  23. return errorValue;
  24. }
  25. public static double ObjToMoney(this object thisValue)
  26. {
  27. double reval = 0;
  28. if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
  29. {
  30. return reval;
  31. }
  32. return 0;
  33. }
  34. public static double ObjToMoney(this object thisValue, double errorValue)
  35. {
  36. double reval = 0;
  37. if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
  38. {
  39. return reval;
  40. }
  41. return errorValue;
  42. }
  43. public static string ObjToString(this object thisValue)
  44. {
  45. if (thisValue != null) return thisValue.ToString().Trim();
  46. return "";
  47. }
  48. public static string ObjToString(this object thisValue, string errorValue)
  49. {
  50. if (thisValue != null) return thisValue.ToString().Trim();
  51. return errorValue;
  52. }
  53. public static Decimal ObjToDecimal(this object thisValue)
  54. {
  55. Decimal reval = 0;
  56. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  57. {
  58. return reval;
  59. }
  60. return 0;
  61. }
  62. public static Decimal ObjToDecimal(this object thisValue, decimal errorValue)
  63. {
  64. Decimal reval = 0;
  65. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  66. {
  67. return reval;
  68. }
  69. return errorValue;
  70. }
  71. public static DateTime ObjToDate(this object thisValue)
  72. {
  73. var reval = DateTime.MinValue;
  74. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  75. {
  76. reval = Convert.ToDateTime(thisValue);
  77. }
  78. return reval;
  79. }
  80. public static DateTime ObjToDate(this object thisValue, DateTime errorValue)
  81. {
  82. var reval = DateTime.MinValue;
  83. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  84. {
  85. return reval;
  86. }
  87. return errorValue;
  88. }
  89. public static bool ObjToBool(this object thisValue)
  90. {
  91. var reval = false;
  92. if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
  93. {
  94. return reval;
  95. }
  96. return reval;
  97. }
  98. }
  99. }