How can I make an if if Database data is True or False

Asked

Viewed 39 times

-1

How can I make an if by checking whether the data of a column in the Database table is True or False?

For example:

using (SqlCommand cmd = new SqlCommand($"SELECT LoginID,Award FROM Sys_Usuarios_Award WHERE LoginID={login.LoginId} and Award='true ou false'", connection))

What I have in mind:

if (Award == True){
Console.WriteLine("True");
}else{
Console.WriteLine("False");
}

Award is the column I picked up at Sqlcommand. Summing up wanted to make an if with the bit data of a column but don’t know how to do.

  • If the column is bit, true=1 and false=0, so something like and (Award=1 or Award=0) but I didn’t understand the purpose of this OR, since the field can only assume these values... it is nullable? Can explain better?

  • It’s just for example, it can be both true and false, I just want to make an if, if it’s True, Console.Writeline("True"); if it’s False Console.Writeline("False")

  • I thought you were talking about how to do query, but the code depends on how your data comes, DataReader, DataTable, etc.

1 answer

1


One simple way is using the Sqldatareader, example below:

using (var comando = connection.CreateCommand())
{
    comando.CommandText = $"SELECT LoginID,Award FROM Sys_Usuarios_Award WHERE LoginID={login.LoginId}";
    var reader = comando.ExecuteReader();

    if (reader.Read())
    {
        if ((bool)reader["Award"])
        {
            Console.WriteLine("True");
        }
        else
        {
            Console.WriteLine("False");
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.