是否可以从数据行中读取值?
我有这段代码:
bool CatchweightItem;
if (dr_art_line["CatchweightItemt"].ToString() -> READ VALUE)
{
CatchweightItem = false;
}
else
{
CatchweightItem = true;
}
所以我想知道是否可以读取该字段的值。如果值为false
,则将变量设置为false
,如果为真,则将变量设置为true
。
答案 0 :(得分:1)
bool CatchweightItem = Convert.ToBoolean(dr_art_line["CatchweightItemt"].ToString());
阅读this
答案 1 :(得分:1)
您必须使用==运算符来检查值,如下所示:
bool CatchweightItem;
if (dr_art_line["CatchweightItemt"].ToString() == "FALSE")
{
CatchweightItem = false;
}
else
{
CatchweightItem = true;
}
实际上,你不需要所有的代码,你也可以做得更短更清洁:
bool CatchweightItem = (dr_art_line["CatchweightItemt"].ToString() == "TRUE")
答案 2 :(得分:1)
如果DataColumn的DataType
仍为bool
,则应使用此强类型方法:
bool isCatchweightItem = dr_art_line.Field<bool>("CatchweightItemt");
DataRowExtensions.Field<T>
Method (DataRow, String)
它还支持可空类型。
答案 3 :(得分:1)
if(string.Compare(Convert.ToString(dr_art_line["CatchweightItemt"])+"","false",true)==0)
{
CatchweightItem = false; }
else {
CatchweightItem = true; }
这将避免来自数据库的空值以及不区分大小写的检查。