C#将位转换为布尔值 [英] C# convert bit to boolean

查看:47
本文介绍了C#将位转换为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Microsoft SQL Server 数据库,其中包含 BIT 类型的数据字段.

I have a Microsoft SQL Server database that contains a data field of BIT type.

此字段将具有 01 值来表示 false 和 true.

This field will have either 0 or 1 values to represent false and true.

我希望在检索数据时将我得到的值转换为 falsetrue 而不使用 if-condition 将数据转换为 false 如果是 0true 如果是 1.

I want when I retrieve the data to convert the value 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 查询,这可能取决于.例如,如果您有 数据阅读器,您可以直接读取一个布尔值:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆