我有一个存储过程,我在其中验证某些列以返回值1或0.
存储的proc代码块是:
, CASE WHEN
(PolicyStartMileage < LossMileage)
AND (PolicyStartDate <= LossDate)
AND (Certified = 0)
THEN '1'
ELSE '0'
END
AS Certifiable
我的问题是,如何将“1”或“0”作为位类型返回?
在我的代码隐藏中,我正在使用:
Boolean.Parse(item.GetDataKeyValue("Certifiable").ToString());
为了读取值(true或false)。
答案 0 :(得分:4)
如果您希望数据是真正的BIT数据类型,则需要进行强制转换,否则它将默认为数字。
CASE WHEN
(PolicyStartMileage < LossMileage)
AND (PolicyStartDate <= LossDate)
AND (Certified = 0)
THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT)
END
AS Certifiable
如果进行强制转换,可以直接从数据读取器调用GetBoolean()方法,避免解析
答案 1 :(得分:1)
, CASE WHEN
(PolicyStartMileage < LossMileage)
AND (PolicyStartDate <= LossDate)
AND (Certified = 0)
THEN 1
ELSE 0
END
AS Certifiable
当您使用1或0左右的引号时,它会将其视为字符串而不是 整数。强>
答案 2 :(得分:1)
尝试删除''
周围的引号1 and 0
,使其表现为integers
而不是string
。像这样:
, CASE WHEN
(PolicyStartMileage < LossMileage)
AND (PolicyStartDate <= LossDate)
AND (Certified = 0)
THEN 1
ELSE 0
END
AS Certifiable