问候.
我有MS SQL服务器数据库那个BIT类型的内容数据字段.
本场将有两种0或1值存在虚假和真实.
我希望当我检索数据以转换我有false或true没有使用if-condition的值来转换数据时,false如果它是0或true如果它1.
我想知道C#中是否有一个函数会直接通过传递位值来做到这一点?
Rob*_*Day 34
DataReader.GetBoolean(x)
Run Code Online (Sandbox Code Playgroud)
要么
Convert.ToBoolean(DataRow[x])
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 29
根据您执行SQL查询的方式,它可能取决于您.例如,如果您有数据读取器,则可以直接读取布尔值:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Luk*_*keH 13
你是如何从数据库中提取字段的?
该SqlDataReader班有GetBoolean其不翻译为你的方法:
bool yourBoolean = reader.GetBoolean(reader.GetOrdinal("Your_Bit_Column"));
Run Code Online (Sandbox Code Playgroud)