//
    /// 类型转换类
    /// 处理数据库获取字段为空的情况
    ///
    public static class DBConvert
    {
        #region------------------ToInt32类型转换------------------
        ///
        /// 读取数据库中字符串并转换成Int32
        /// 为空时返回0
        ///
        /// object类型的值
        /// Int32类型
        public static int ToInt32(object obj)
        {
            int result = 0;
            if (IsInt(Convert.ToString(obj)))
            {
                result = Convert.ToInt32(obj);
            }
            else if (obj != null && obj is Enum) //处理非null值类型时(或者枚举)
            {
                result = ((IConvertible)obj).ToInt32(null);
            }
            return result;
        }
 
        ///
        /// 读取数据库中字符串并转换成Int32
        /// 为空时返回0
        ///
        /// string类型的值
        /// Int32类型
        public static int ToInt32(string str)
        {
            int result = 0;
            if (IsInt(str))
            {
                result = Convert.ToInt32(str);
            }
            return result;
        }
 
        ///
        /// 判断一个字符串是否属于Int类型
        /// 如果是的返回true,如果不是返回false
        ///
        /// string类型的值
        /// true:是Int的字符串(即可以转换成Int类型),false:不是Int类型的字符串
        public static bool IsInt(string str)
        {
            bool result = false;
            if (str != "" && str!=null)
            {
                Regex reg = new Regex("^[0-9]*$");
                if (reg.IsMatch(str))
                {
                    result = true;
                }
            }
            return result;
        }
        #endregion
 
        #region------------------ToString类型转换------------------
        ///
        ///  读取数据库中字符串并转换成string
        ///
        /// object类型的值
        /// string类型
        public static string ToString(object obj)
        {
            string result = "";
            if (obj != null)
            {
                result = Convert.ToString(obj);
            }
            return result;
        }
        #endregion
 
        #region------------------ToDouble类型转换------------------
        ///
        /// 判断一个字符串是否属于Double类型(包括负浮点型)
        /// 如果是的返回true,如果不是返回false
        ///
        /// string类型的值
        /// true:是Double的字符串(即可以转换成Double类型),false:不是Double类型的字符串
        public static bool IsDouble(string str)
        {
            bool result = false;
            if (str != "" && str != null)
            {
                Regex reg = new Regex(@"^(-?\d+)(\.\d+)?$");
                if (reg.IsMatch(str))
                {
                    result = true;
                }
            }
            return result;
        }
 
        ///
        /// 读取数据库中字符串并转换成Int32
        /// 为空时返回0
        ///
        /// object类型的值
        /// Int32类型
        public static double ToDouble(object obj)
        {
            double result = 0.0;
            if (IsDouble(Convert.ToString(obj)))
            {
                result = Convert.ToDouble(obj);
            }
            else if (obj != null && obj is Enum) //处理枚举
            {
                result = ((IConvertible)obj).ToDouble(null);
            }
            return result;
        }
 
        ///
        /// 读取数据库中字符串并转换成Int32
        /// 为空时返回0
        ///
        /// string类型的值
        /// Int32类型
        public static double ToDouble(string str)
        {
            double result = 0.0;
            if (IsDouble(str))
            {
                result = Convert.ToDouble(str);
            }
            return result;
        }
        #endregion
 
        #region------------------ToDateTime类型转换------------------
        ///
        /// 判断时间格式是否是时间类型
        /// 如23:10
        ///
        /// 要判断的字符串
        /// true:是时间类型的字符串(即可以转换成时间类型),false:不是时间类型的字符串
        public static bool isDateTime(string str)
        {
            bool result = false;
            if (str != "" && str != null)
            {
                Regex reg = new Regex("(([01]\\d)|(2[0-3])):[0-5]\\d");
                if (reg.IsMatch(str))
                {
                    result = true;
                }
            }
            return result;
        }
        #endregion
 
    }
}
//"^\d+(\.\d+)?$"  //非负浮点数(正浮点数 + 0)
//"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数
//"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮点数(负浮点数 + 0)
//"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数
//"^(-?\d+)(\.\d+)?$"  //浮点数

最新文章

  1. Asp.Net MVC4 + Oracle + EasyUI 学习 第二章
  2. android studio 注释模板
  3. 【leetcode】Anagrams
  4. chm格式文件能打开,但看不到内容问题
  5. Java文件解压之TGZ解压
  6. 【转】怎样将DataGridView中绑定的表的列名改成中文
  7. java从mysql导出数据例子
  8. Android 小闹钟程序
  9. 三十二、Java图形化界面设计——布局管理器之CardLayout(卡片布局)
  10. CloseHandle(),TerminateThread(),ExitThread()的区别
  11. 用js编解码base64
  12. pytthon—day8 读写模式的结合、文件操作模式、with完成文本文件复制、游标操作
  13. 最大流 USTC1280
  14. 手把手制作一个简单的IDEA插件(环境搭建Demo篇)
  15. An error occurred. Sorry, the page you are looking for is currently unavailable. Please try again later.
  16. 泛型-----键值对----映射 hashmap--entry中key value 链表
  17. python解析json数据
  18. 6、JVM--类文件结构(下)
  19. Spring MVC 中 AJAX请求并返回JSON
  20. idea 类注释,方法注释设置

热门文章

  1. java_day10_多线程
  2. Oracle【账户管理】
  3. string类的总结
  4. 两步创建vue全局组件
  5. CF #546 D.E
  6. maven命令创建web骨架项目
  7. JSP+Servlet+DAO+Javabean模式小记-20171029
  8. Vue学习日记(四)——Vue状态管理vuex
  9. python2.6切换python3.4的操作记录
  10. Python之signal模块的使用