C# 从 DataTable 获取一个布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1050663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:02:07  来源:igfitidea点击:

Get a boolean value from DataTable

c#.net-1.1

提问by Developer

How do I retrieve a Boolean value in dataset, I'm using visual studio 2003,I am trying the following, but it's not working:

如何检索数据集中的布尔值,我使用的是 Visual Studio 2003,我正在尝试以下操作,但它不起作用:

//if product inactive, don't display, and redirect to main page
  if((dbDataSet.Tables["productGeneral"].Rows[0]["Active"].Equals(0)))

I even tried, but not working:

我什至尝试过,但不起作用:

if((dbDataSet.Tables["productGeneral"].Rows[0]["Active"].toString() == false)

the columns name is ["active"], the value with in column is either True or False, using sql server 2000

列名称为 ["active"],列中的值为 True 或 False,使用 sql server 2000

please help

请帮忙

采纳答案by Reed Copsey

You need to cast to a bool directly, and just check using that.

您需要直接转换为 bool,然后使用它进行检查。

Have you tried?:

你有没有尝试过?:

if(((bool)dbDataSet.Tables["productGeneral"].Rows[0]["Active"] == false))

If it's a bool, you'll want to cast the result to a bool directly.

如果它是一个 bool,您将希望直接将结果转换为一个 bool。

The first fails since 0 is an Int32, not a Boolean. They are not comparable in C#, since they're distinct types. The second fails since ToString() turns the result into a string, and you're comparing a string to a bool, which again will not work.

第一个失败,因为 0 是一个 Int32,而不是一个布尔值。它们在 C# 中不可比较,因为它们是不同的类型。第二个失败,因为 ToString() 将结果转换为字符串,并且您将字符串与 bool 进行比较,这再次不起作用。

回答by pedrofernandes

Try out the following

试试下面的

   if (Convert.ToBoolean(dbDataSet.Tables["productGeneral"].Rows[0]["Active"]) == true)
   {}