using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ant.ORM { public abstract class BaseModel { protected string ObjectToString(object obj) { return DBNull.Value.Equals(obj) ? string.Empty : obj.ToString(); } protected decimal ObjectToDecimal(object obj) { return DBNull.Value.Equals(obj) ? 0 : Convert.ToDecimal(obj); } protected Int64 ObjectToInt64(object obj) { return DBNull.Value.Equals(obj) ? 0 : Convert.ToInt64(obj); } protected UInt64 ObjectToUInt64(object obj) { return DBNull.Value.Equals(obj) ? 0 : Convert.ToUInt64(obj); } protected Int32 ObjectToInt32(object obj) { return DBNull.Value.Equals(obj) ? 0 : Convert.ToInt32(obj); } protected UInt32 ObjectToUInt32(object obj) { return DBNull.Value.Equals(obj) ? 0 : Convert.ToUInt32(obj); } protected Int16 ObjectToInt16(object obj) { return DBNull.Value.Equals(obj) ? (Int16)0 : Convert.ToInt16(obj); } protected UInt16 ObjectToUInt16(object obj) { return DBNull.Value.Equals(obj) ? (UInt16)0 : Convert.ToUInt16(obj); } protected sbyte ObjectToSByte(object obj) { return DBNull.Value.Equals(obj) ? (sbyte)0 : Convert.ToSByte(obj); } protected bool ObjectToBoolean(object obj) { return DBNull.Value.Equals(obj) ? false : Convert.ToBoolean(obj); } protected DateTime ObjectToDateTime(object obj) { return DBNull.Value.Equals(obj) ? DateTime.MinValue : Convert.ToDateTime(obj); } protected Guid ObjectToGuid(object obj) { return Guid.Empty.Equals(obj) ? Guid.Empty : new Guid(Convert.ToString(obj)); } protected byte[] ObjectToBytes(object obj) { return DBNull.Value.Equals(obj) ? null : (byte[])obj; } #region 返回DataType DbNll public object SqlNull(object obj) { if (obj == null) { return obj; } string tp = string.Empty; tp = obj.GetType().ToString(); switch (tp) { case "System.Guid": if (obj.ToString() == Guid.Empty.ToString()) { return DBNull.Value; } break; case "System.DateTime": if (obj.ToString() == new DateTime().ToString()) { return DBNull.Value; } break; case "System.Int32": if (obj.ToString() == "0") { return DBNull.Value; } else if (obj.ToString() == "-1") { return 0; } break; case "System.Decimal": if (obj.ToString() == "0.00") { return DBNull.Value; } break; } return obj; } #endregion } }