数据转换扩展类

    /// <summary>
    /// 数据转换扩展类型
    /// </summary>
    public static class DataConvertExtend
    {
        /// <summary>
        /// 对象转成字符
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>字符</returns>
        public static string DataToString(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return string.Empty;
            }

            return obj.ToString();
        }

        /// <summary>
        /// 对象转成整型
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>整型</returns>
        public static int DataToInt(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return 0;
            }

            int data = int.MinValue;
            if (int.TryParse(obj.ToString(), out data) == false)
            {
                return 0;
            }

            return data;
        }

        /// <summary>
        /// 对象转换成整型
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>整型</returns>
        public static int? DataToIntMark(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return null;
            }

            int data = int.MinValue;
            if (int.TryParse(obj.ToString(), out data) == false)
            {
                return null;
            }

            return data;
        }

        /// <summary>
        /// 对象转成Float
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>Float</returns>
        public static float DataToFloat(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return 0;
            }

            float data = float.MinValue;
            if (float.TryParse(obj.ToString(), out data) == false)
            {
                return 0;
            }

            return data;
        }

        /// <summary>
        /// 对象转换成Float
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>Float</returns>
        public static float? DataToFloatMark(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return null;
            }

            float data = float.MinValue;
            if (float.TryParse(obj.ToString(), out data) == false)
            {
                return null;
            }

            return data;
        }

        /// <summary>
        /// 对象转成double
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>double</returns>
        public static double DataToDouble(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return 0;
            }

            double data = double.MinValue;
            if (double.TryParse(obj.ToString(), out data) == false)
            {
                return 0;
            }

            return data;
        }

        /// <summary>
        /// 对象转换成double
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>double</returns>
        public static double? DataToDoubleMark(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return null;
            }

            double data = double.MinValue;
            if (double.TryParse(obj.ToString(), out data) == false)
            {
                return null;
            }

            return data;
        }

        /// <summary>
        /// 对象转成浮点类型
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>浮点类型</returns>
        public static decimal DataToDecimal(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return 0;
            }

            decimal data = decimal.MinValue;
            if (decimal.TryParse(obj.ToString(), out data) == false)
            {
                return 0;
            }

            return data;
        }

        /// <summary>
        /// 对象转换成浮点类型
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>浮点类型</returns>
        public static decimal? DataToDecimalMark(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return null;
            }

            decimal data = decimal.MinValue;
            if (decimal.TryParse(obj.ToString(), out data) == false)
            {
                return null;
            }

            return data;
        }

        /// <summary>
        /// 对象转换成日期类型
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>日期</returns>
        public static DateTime DataToDateTime(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return DateTime.MinValue;
            }

            DateTime date = DateTime.MinValue;
            if (DateTime.TryParse(obj.ToString(), out date) == false)
            {
                return date;
            }

            return date;
        }

        /// <summary>
        /// 对象转换成时间类型
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>时间</returns>
        public static DateTime DataToTime(this object obj)
        {
            DateTime date = DateTime.ParseExact("00:00:00", "HH:mm:ss", null);
            if (obj == null || obj == DBNull.Value)
            {
                return date;
            }

            if (DateTime.TryParse(obj.ToString(), out date) == false)
            {
                date = DateTime.ParseExact(obj.ToString(), "HH:mm:ss", null);
            }
            return date;
        }

        /// <summary>
        /// 对象转换成日期类型
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>日期</returns>
        public static DateTime? DataToDateTimeMark(this object obj)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return null;
            }

            DateTime date = DateTime.MinValue;
            if (DateTime.TryParse(obj.ToString(), out date) == false)
            {
                return null;
            }

            return date;
        }
    }
View Code

 

posted @ 2015-01-23 09:50  zhuifeng0724  阅读(141)  评论(0编辑  收藏  举报