我在select查询中使用case语句这样的东西.
Select col1, col2, isActive = case when col3 = 'abc' then 1 else 0 end, col4
from <tablename> where <some condition>.
Run Code Online (Sandbox Code Playgroud)
当我在ado.net中读取'isActive'列值时 -
bool isActiveFlag = (bool)datareader["isActive"];
Run Code Online (Sandbox Code Playgroud)
我得到了类型转换错误.因为它返回值为int.
如果我将查询更改为
Select col1, col2, isActive = case when col3 = 'abc' then 'true' else 'false' end, col4
from <tablename> where <some condition>.
Run Code Online (Sandbox Code Playgroud)
我得到相同的类型转换错误,因为此时返回的值是字符串.基本上我想查询在查询中为布局值'isActive'赋值,这样我就可以把它读作布尔值.
bool isActiveFlag = (bool)datareader["isActive"];
Run Code Online (Sandbox Code Playgroud)
知道怎么处理这个吗?
我不想将值读取为int或string,然后将值转换为boolean.