Return Datareader to bool field

Asked

Viewed 569 times

4

I need to return a field that is of the type bool:

    public List<TB_USUARIO> ListarTodos()
    {
        var strQuery = "select * from tb_usuario";

        using (contexto = new Contexto())
        {
            var retornoDataReader = contexto.ExecutaComandoComRetorno(strQuery);
            return TransformaReaderEmListaObjetos(retornoDataReader);
        }

    }

    private List<TB_USUARIO> TransformaReaderEmListaObjetos(SqlDataReader reader)
    {
        var retornando = new List<TB_USUARIO>();
        while (reader.Read())
        {

            TB_USUARIO tabela = new TB_USUARIO()
            {
                IDUSUARIO = reader["IDUSUARIO"] == DBNull.Value ? 0 : Convert.ToInt32(reader["IDUSUARIO"]),
                NOME = reader["NOME"] == DBNull.Value ? string.Empty : reader["NOME"].ToString(),
                LOGIN = reader["LOGIN"] == DBNull.Value ? string.Empty : reader["LOGIN"].ToString(),
                SENHA = reader["SENHA"] == DBNull.Value ? string.Empty : reader["SENHA"].ToString(),
                ADMINISTRADOR = (bool)reader["ADMINISTRADOR"] //este campo
        };

            retornando.Add(tabela);
        }
        reader.Close();
        return retornando;
    }
  • What is the value within reader["ADMINISTRADOR"]? Is there a problem? This column may be null? What kind of it?

  • is always coming as false

  • What is the type of column in the database?

  • sql server database char(1) column, but I already have the answer: ADMINISTRATOR = (Reader["ADMINISTRATOR"] as string == "S") ? true : false

  • You don’t need the ? true : false. The sentence (reader["ADMINISTRADOR"] as string == "S") already returns a boolean.

  • @jbueno , thank you for all your help! immensely grateful

Show 1 more comment

2 answers

5


Just do this:

ADMINISTRADOR = reader["ADMINISTRADOR"] as string == "S"; //se tem S ou N

I put in the Github for future reference.

Since the column is not originally a boolean but a text, it should be compared with the text representing the true state.

Although it is late, I advise not to use all uppercase names and prefixes in names.

  • it is not too late, the bank has the fields in uppercase, and the classes in I did following the same pattern, can explain me the problem of this ?

  • 1

    @itasouza It is no problem to keep the properties capitalized, but escapes the C’s naming convention#

  • It’s not a problem, it’s style violation only. NO SQL is even normal (although I don’t like it), but it’s weird to use this in C#. http://answall.com/q/31646/101. And the use of prefix is somewhat redundant and unnecessary, is considered Hungarian notation.

  • 1

    @itasouza A tip based on your comment: languages usually have different conventions, just because SQL adopts this standard, does not mean that C# should be like this. If you consume this data via JS API, JS also has another standard that must be respected. But according to #bigown’s comment, it’s really not a problem, it’s just a violation of style.

  • Although to this day the most common that I have seen in banks is using the underscore. I also see a lot of Pascalcase and camelCase. First time I see UPPERCASE.

3

According to the comments, the column ADMINISTRADOR is the type char and you save the values S/N.

The only explicit string to Boolean conversion you can do is convert "true" (for true) or "false" (for false).

The solution is for you to validate which value exists in the column, in this way

ADMINISTRADOR = (string)reader["ADMINISTRADOR"] == "S"

That, if the column ADMISTRADOR do not accept null, otherwise use the operator as (in that case considering that null corresponds to false.

ADMINISTRADOR = reader["ADMINISTRADOR"] as string == "S"

Browser other questions tagged

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