C#类型转换类(通用类)

//
     /// 类型转换类
     /// 处理数据库获取字段为空的情况
     ///
     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+)?$"  //浮点数

转载于:https://www.cnblogs.com/fengyingwang/p/7591133.html

很多,不写全了。。下载下来好好看 ----------Database-------------- 1.DataTable帮助(DataTableHelper.cs) 2.Access数据库文件操作辅助(JetAccessUtil.cs) 5.查询条件组合辅助(SearchCondition.cs) 6.查询信息实体(SearchInfo.cs) 8.Sql命令操作函数(可用于安装程序的时候数据库脚本执行)(SqlScriptHelper.cs) ----------Device-------------- 声音播放辅助(AudioHelper.cs) 摄像头操作辅助,包括开启、关闭、抓图、设置等功能(Camera.cs) 提供用于操作【剪切板】的方法(ClipboardHelper.cs) 获取电脑信息(Computer.cs) 提供用户硬件唯一信息的辅助(FingerprintHelper.cs) 读取指定盘符的硬盘序列号(HardwareInfoHelper.cs) 提供访问键盘当前状态的属性(KeyboardHelper.cs) 全局键盘钩子。这可以用来在全球范围内捕捉键盘输入。(KeyboardHook.cs) 模拟鼠标点击(MouseHelper.cs) 全局鼠标钩子。这可以用来在全球范围内捕获鼠标输入。(MouseHook.cs) MP3文件播放操作辅助(MP3Helper.cs) 关联文件(ExtensionAttachUtil.cs) 注册文件关联的辅助(FileAssociationsHelper.cs) 打开、保存文件对话框操作辅助(FileDialogHelper.cs) 常用的文件操作辅助FileUtil(FileUtil.cs) INI文件操作辅助(INIFileUtil.cs) 独立存储操作辅助(IsolatedStorageHelper.cs) 序列号操作辅助(Serializer.cs) 获取一个对象,它提供用于访问经常引用的目录的属性。(SpecialDirectories.cs) 简单的Word操作对象(WordCombineUtil.cs) 这个提供了一些实用的方法来转换XML和对象。(XmlConvertor.cs) XML操作(XmlHelper.cs) ----------Format-------------- 参数验证的通用验证程序。(ArgumentValidation.cs) 这个提供了实用方法的字节数组和图像之间的转换。(ByteImageConvertor.cs) byte字节数组操作辅助(BytesTools.cs) 处理数据类型转换,数制转换、编码转换相关的(ConvertHelper.cs) CRC校验辅助(CRCUtils.cs) 枚举操作公共(EnumHelper.cs) 身份证操作辅助(IDCardHelper.cs) 检测字符编码的(IdentifyEncoding.cs) RGB颜色操作辅助(MyColors.cs) 日期操作(MyDateTime.cs) 转换人民币大小金额辅助(RMBUtil.cs) 常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅助(ValidateUtil.cs) ----------Network-------------- Cookie操作辅助(CookieManger.cs) FTP操作辅助(FTPHelper.cs) HTML操作(HttpHelper.cs) 网页抓取帮助(HttpWebRequestHelper.cs) Net(NetworkUtil.cs) IE代理设置辅助(ProxyHelper.cs) ----------Winform-------------- 跨线程的控件安全访问方式(CallCtrlWithThreadSafety.cs) CheckBoxList(CheckBoxListUtil.cs) 窗口管理(ChildWinManagement.cs) 由马丁·米勒http://msdn.microsoft.com/en-us/library/ms996492.aspx提供一个简单的方法打印工作的一个RichTextBox一个帮手(ExRichTextBoxPrintHelper.cs) 显示,隐藏或关闭动画形式。(FormAnimator.cs) 对窗体进行冻结、解冻操作辅助(FreezeWindowUtil.cs) 窗体全屏操作辅助(FullScreenHelper.cs) GDI操作辅助(GDI.cs) 提供静态方法来读取这两个文件夹和文件的系统图标。(IconReaderHelper.cs) 图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助(ImageHelper.cs) 输入法帮助,全角 转换为半角(ImeHelper.cs) Winform提示框 的摘要说明。(MessageUtil.cs) 包含互操作方法调用的应用程序中使用。(NativeMethods.cs) 托盘图标辅助(NotifyIconHelper.cs) 打印机(POSPrinter.cs) 图片、光标、图标、位图等资源操作辅助(ResourceHelper.cs) RTF字符格式辅助(RTFUtility.cs) 串口开发辅助(SerialPortUtil.cs) 设置文本属性提供一个ToolStripStatusLabel(SafeToolStripLabel.cs) 只运行一个实例及系统自动启动辅助(StartupHelper.cs) Web页面预览效果图片抓取辅助(WebPageCapture.cs) 供Asp.Net直接调用的包装(WebPreview.cs) 计算机重启、关电源、注销、关闭显示器辅助(WindowsExitHelper.cs) 简单写了点,还有很多,希望能对大家有帮助 ================================================================================================ 本资料共包含以下附件: WHC.OrderWater.Commons.rar 公共文档.docx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值