BaseModel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Ant.ORM
  6. {
  7. public abstract class BaseModel
  8. {
  9. protected string ObjectToString(object obj)
  10. {
  11. return DBNull.Value.Equals(obj) ? string.Empty : obj.ToString();
  12. }
  13. protected decimal ObjectToDecimal(object obj)
  14. {
  15. return DBNull.Value.Equals(obj) ? 0 : Convert.ToDecimal(obj);
  16. }
  17. protected Int64 ObjectToInt64(object obj)
  18. {
  19. return DBNull.Value.Equals(obj) ? 0 : Convert.ToInt64(obj);
  20. }
  21. protected UInt64 ObjectToUInt64(object obj)
  22. {
  23. return DBNull.Value.Equals(obj) ? 0 : Convert.ToUInt64(obj);
  24. }
  25. protected Int32 ObjectToInt32(object obj)
  26. {
  27. return DBNull.Value.Equals(obj) ? 0 : Convert.ToInt32(obj);
  28. }
  29. protected UInt32 ObjectToUInt32(object obj)
  30. {
  31. return DBNull.Value.Equals(obj) ? 0 : Convert.ToUInt32(obj);
  32. }
  33. protected Int16 ObjectToInt16(object obj)
  34. {
  35. return DBNull.Value.Equals(obj) ? (Int16)0 : Convert.ToInt16(obj);
  36. }
  37. protected UInt16 ObjectToUInt16(object obj)
  38. {
  39. return DBNull.Value.Equals(obj) ? (UInt16)0 : Convert.ToUInt16(obj);
  40. }
  41. protected sbyte ObjectToSByte(object obj)
  42. {
  43. return DBNull.Value.Equals(obj) ? (sbyte)0 : Convert.ToSByte(obj);
  44. }
  45. protected bool ObjectToBoolean(object obj)
  46. {
  47. return DBNull.Value.Equals(obj) ? false : Convert.ToBoolean(obj);
  48. }
  49. protected DateTime ObjectToDateTime(object obj)
  50. {
  51. return DBNull.Value.Equals(obj) ? DateTime.MinValue : Convert.ToDateTime(obj);
  52. }
  53. protected Guid ObjectToGuid(object obj)
  54. {
  55. return Guid.Empty.Equals(obj) ? Guid.Empty : new Guid(Convert.ToString(obj));
  56. }
  57. protected byte[] ObjectToBytes(object obj)
  58. {
  59. return DBNull.Value.Equals(obj) ? null : (byte[])obj;
  60. }
  61. #region 返回DataType DbNll
  62. public object SqlNull(object obj)
  63. {
  64. if (obj == null)
  65. {
  66. return obj;
  67. }
  68. string tp = string.Empty;
  69. tp = obj.GetType().ToString();
  70. switch (tp)
  71. {
  72. case "System.Guid":
  73. if (obj.ToString() == Guid.Empty.ToString())
  74. {
  75. return DBNull.Value;
  76. }
  77. break;
  78. case "System.DateTime":
  79. if (obj.ToString() == new DateTime().ToString())
  80. {
  81. return DBNull.Value;
  82. }
  83. break;
  84. case "System.Int32":
  85. if (obj.ToString() == "0")
  86. {
  87. return DBNull.Value;
  88. }
  89. else if (obj.ToString() == "-1")
  90. {
  91. return 0;
  92. }
  93. break;
  94. case "System.Decimal":
  95. if (obj.ToString() == "0.00")
  96. {
  97. return DBNull.Value;
  98. }
  99. break;
  100. }
  101. return obj;
  102. }
  103. #endregion
  104. }
  105. }