123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- 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
- }
- }
|