How to write bit information to SQL Server database?

Asked

Viewed 329 times

3

I am developing a VB registration system and need help to record the information of a CheckBox in the SQL Server database.

The column is in bit in the database, I don’t 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
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

3

Do this:

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

I put in the Github for future reference.

The property is a booliano, has a true or false. Contrary to what the guy might look like BIT SQL Server is an integer that only allows the values 0 and 1 and that can have some storage optimization. Then you have to convert the boolean to integer to record. And when you read it you will have to transform this integer into boolean. In some contexts there are other possible solutions, but I tried not to invent and just do what you are asking and showing.

  • Thanks bigown, it worked out, only a doubt had to convert the Int Checkbox property to bit?

  • 1

    The opposite. The property is a boolean, has a true or false. Contrary to what the guy might look like BIT SQL Server is an integer that only allows the values 0 and 1 and that can have some storage optimization. Then you have to convert the boolean to integer to record. And when you read it you will have to turn this integer into boolean. In some contexts there are other possible solutions, but I tried not to invent and just do what you are asking and showing.

Browser other questions tagged

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