csharp: DBNull and DateTime_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > csharp: DBNull and DateTime

csharp: DBNull and DateTime

 2016/5/25 5:35:28  ?Geovin Du Dream Park?  程序员俱乐部  我要评论(0)
  • 摘要:///<summary>//////</summary>///<paramname="dateTime"></param>///<returns></returns>publicDateTimeCheckDBNull(objectdateTime){if(dateTime==DBNull.Value)returnDateTime.MinValue;elsereturn(DateTime)dateTime;}///<
  • 标签:ARP
class="brush:csharp;gutter:true;">/// <summary>
        /// 
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public DateTime CheckDBNull(object dateTime)
        {
            if (dateTime == DBNull.Value)
                return DateTime.MinValue;
            else
                return (DateTime)dateTime;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public DateTime? ReadNullableDateTimefromReader(string field, IDataRecord data)
        {

            var a = data[field];
            if (a != DBNull.Value)
            {
                return Convert.ToDateTime(a);
            }
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="field"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public DateTime ReadDateTimefromReader(string field, IDataRecord data)
        {
            DateTime value;
            var valueAsString = data[field].ToString();
            try
            {
                value = DateTime.Parse(valueAsString);
            }
            catch (Exception)
            {
                throw new Exception("Cannot read Datetime from reader");
            }

            return value;
        }
	}

  

menu.IsMainMenu = (!DBNull.Equals(reader["IsMainMenu"], null)) ? (bool)reader["IsMainMenu"] : true;
                        menu.Remark = (!DBNull.Equals(reader["Remark"], null)) ? (string)reader["Remark"].ToString() : "";
                        menu.ParentMenuId = (!DBNull.Equals(reader["ParentMenuId"], null)) ? (int)reader["ParentMenuId"] : 0;
                        menu.TreeLevel = (!DBNull.Equals(reader["TreeLevel"], null)) ? (int)reader["TreeLevel"] : 0;
                        menu.SortIndex = (!DBNull.Equals(reader["SortIndex"], null)) ? (int)reader["SortIndex"] : 0;
                        menu.CreatedOn = CheckDBNull(reader["CreatedOn"]);// (!object.Equals(reader["CreatedOn"], null)) ? (DateTime)reader["CreatedOn"] : DateTime.Now;

  

发表评论
用户名: 匿名