C# convert bit to boolean(C#将位转换为布尔值)
问题描述
我有一个 Microsoft SQL Server 数据库,其中包含 BIT
类型的数据字段.
I have a Microsoft SQL Server database that contains a data field of BIT
type.
此字段将具有 0
或 1
值来表示 false 和 true.
This field will have either 0
or 1
values to represent false and true.
我希望在检索数据时将我得到的值转换为 false
或 true
而不使用 if-condition 将数据转换为 false
如果是 0
或 true
如果是 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#将位转换为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C#将位转换为布尔值


基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01