如何将对象从数据读取器转换为布尔值? [英] How do I cast object from data reader to boolean value ?

查看:122
本文介绍了如何将对象从数据读取器转换为布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将对象从数据库转换为布尔值。



这是无效的案件。



How do I cast object to Boolean value from database.

this is throwing invalid case excetion.

emp.IsActive = (bool)rdr["Is_Active"];
emp.IsActive = Convert.ToBoolean(rdr["Is_Active"]);





我试过的:





What I have tried:

emp.IsActive = (bool)rdr["Is_Active"];
emp.IsActive = Convert.ToBoolean(rdr["Is_Active"]);

推荐答案

check Boolean.TryParse Method(String,Boolean)(System) [< a href =https://msdn.microsoft.com/en-us/library/system.boolean.tryparse(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]

check Boolean.TryParse Method (String, Boolean) (System)[^]
bool isActive = false;
          if (bool.TryParse(rdr["Is_Active"] + "", out isActive)) {
              emp.IsActive = isActive
          }
          else
          {
             // show the error message to the user
          }


这篇关于如何将对象从数据读取器转换为布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆