我想选择布尔值:
SELECT field1, field2, 1 as is_field
FROM TABLE
在Visual Studio 2010中我做的:
In Visual Studio 2010 I am doing:
bool b = row.Field<bool>("is_field");
不过,我得到异常:
But I get the exception:
指定的转换是无效的。
我怎样才能返回一个布尔值布尔
,而不是 INT
?
How can I return a boolean value as boolean
and not as int
?
从SQL的一面,你可以这样做:
From SQL side, you can do:
SELECT field1, field2, CAST(1 AS BIT) AS is_field
FROM TABLE
要迫使它返回为位,而不是一个int。这应该做的伎俩
to force it to be returned as a BIT instead of an int. That should do the trick
更新: 什么是你的关心使用CAST这样吗? SQL Server是pretty的真棒在优化例如对于上面的查询会显示is_field的执行计划是一个常数,可以计算一次达阵(这里有一个相关文章:http://msdn.microsoft.com/en-us/library/ms175933.aspx).即使没有,如果你关心性能,那么你不应该担心 - 这将非常有premature优化
Update: What is your concern for using CAST like this? SQL Server is pretty awesome at optimising e.g. the execution plan for above query would show is_field is a Constant which can be evaluated once up front (there's a related article here: http://msdn.microsoft.com/en-us/library/ms175933.aspx). Even without that, if you're concerned about performance, then you shouldn't worry about it - it would very much be premature optimisation.
上一篇:使用LINQ to选择类返回的IEnumerable&LT的性质; T&GT;性质、to、LINQ、IEnumerable
下一篇:通过.NET Framework类以编程方式使用xsd.exe工具的功能(生成XSD架构的类)?架构、方式、功能、工具