How to write bit information to SQL Server database?

3

I am developing a registry system in VB and need help writing the CheckBox information to the SQL Server database.

The column is in bit in the database, I do not know how to pass the parameter so that the user's choice is saved in the database.

In the case of Nchar use the following parameter:

.Parameters.Add(New SqlParameter("@Nome", SqlDbType.Nchar)).Value = txtNome.Text

But in the case of bit I'm doing something wrong:

.Parameters.Add(New SqlParameter("@Ativo", SqlDbType.bit)).Value = chbAtivo.Checked
    
asked by anonymous 25.09.2015 / 16:52

1 answer

3

Do this:

Parameters.Add(New SqlParameter("@Ativo", SqlDbType.bit)).Value =
                                             Convert.ToInt32(chbAtivo.Checked)

The property is a Boolean, has a true or false . Contrary to what may seem like the type BIT of SQL Server is an integer that only allows values 0 and 1 and may have some storage optimization. Then you have to convert the booliano to integer to record. And when you read it you'll have to turn this whole into a Boolean. In some contexts there are other possible solutions, but I tried not to invent and just do what you are asking for and showing.

    
25.09.2015 / 17:11