C#转换位为布尔 [英] C# convert bit to boolean
问题描述
问候。
我有MS SQL服务器数据库中的内容数据字段 BIT
键入
I have MS SQL server database that content data field of BIT
type.
这个领域将有两种 0
或 1
值,以present虚假的和真实的。
this field will have either 0
or 1
values to present false and true.
我想,当我检索数据转换价值练到假
或真
不,如果使用-condition将数据转换为假
如果是 0
或真
如果是 1
。
I want when I retrieve the data to convert the value of I got to false
or true
without using if-condition to convert the data to false
if it is 0
or true
if it is 1
.
我不知道是否有在C#中的函数会做这个,直接传递位的值吗?
I'm wondering if there is a function in C# would do this direct by passing bit values to it?
推荐答案
根据你是如何执行的SQL查询可能依赖。例如,如果你有一个数据读取器,请直接<一href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getboolean%28VS.80%29.aspx">read一个布尔值:
Depending on how are you performing the SQL queries it may depend. For example if you have a data reader you could directly read a boolean value:
using (var conn = new SqlConnection(ConnectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SELECT isset_field FROM sometable";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
bool isSet = reader.GetBoolean(0);
}
}
}
这篇关于C#转换位为布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!